Table
CREATE TABLE [dbo].[YOUR TABLE NAME] (
[UserName] NVARCHAR (50) NULL,
[Password] NVARCHAR (50) NULL,
[SecurityQuestion] NVARCHAR (MAX) NULL,
[SecurityAnswer] NVARCHAR (MAX) NULL
);
Stored Procedure
CREATE PROCEDURE ValidateUser
(
@User_name VARCHAR(50)
,@User_password VARCHAR(50)
)
AS
BEGIN
SELECT UserName
,Password
FROM Login_Details
WHERE UserName=@User_name AND Password=@User_password
END
Login Code
private void button1_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please provide User Name", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (txtPwd.Text == "")
{
MessageBox.Show("Please provide Password", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
SqlCommand cmd = new SqlCommand("sp_ValidateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@User_name ", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@User_password ", txtPwd.Text.Trim());
adp.SelectCommand = cmd;
adp.Fill(dt);
cmd.Dispose();
if (dt.Rows.Count > 0)
{
MainForm frm2 = new MainForm();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
this.Hide();
}
else
{
MessageBox.Show("User Name or Password is wrong", "Please check", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
txtUserName.Text = "";
txtPwd.Text = "";
txtUserName.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
dt.Clear();
dt.Dispose();
adp.Dispose();
}
}
}
Connection String
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["nameofconnectionstring"].ConnectionString);
App.Config
providerName="System.Data.SqlClient" />
>
CREATE TABLE [dbo].[YOUR TABLE NAME] (
[UserName] NVARCHAR (50) NULL,
[Password] NVARCHAR (50) NULL,
[SecurityQuestion] NVARCHAR (MAX) NULL,
[SecurityAnswer] NVARCHAR (MAX) NULL
);
Stored Procedure
CREATE PROCEDURE ValidateUser
(
@User_name VARCHAR(50)
,@User_password VARCHAR(50)
)
AS
BEGIN
SELECT UserName
,Password
FROM Login_Details
WHERE UserName=@User_name AND Password=@User_password
END
Login Code
private void button1_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please provide User Name", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (txtPwd.Text == "")
{
MessageBox.Show("Please provide Password", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
SqlCommand cmd = new SqlCommand("sp_ValidateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@User_name ", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@User_password ", txtPwd.Text.Trim());
adp.SelectCommand = cmd;
adp.Fill(dt);
cmd.Dispose();
if (dt.Rows.Count > 0)
{
MainForm frm2 = new MainForm();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
this.Hide();
}
else
{
MessageBox.Show("User Name or Password is wrong", "Please check", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
txtUserName.Text = "";
txtPwd.Text = "";
txtUserName.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
dt.Clear();
dt.Dispose();
adp.Dispose();
}
}
}
Connection String
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["nameofconnectionstring"].ConnectionString);
App.Config
>
No comments:
Post a Comment