pls refer this code
using System.Configuration;
using System.Data.SqlClient;
private void btnLogin_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtUserName.Text) == true || string.IsNullOrEmpty(txtUserPass.Text) == true)
{
MessageBox.Show("Please Provide Missing Details! ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (SqlConnection con = new SqlConnection(constr))
{
try
{
SqlCommand cmd = new SqlCommand("select UserName,UserPassword from tblUsers where UserName=@UserName and UserPassword=@UserPass", con);
cmd.Parameters.Add(new SqlParameter("@UserName", this.txtUserName.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@UserPass", this.txtUserPass.Text.Trim()));
con.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (rdr.Read() == true)
{
this.Hide();
frmMain frm = new frmMain();
frm.Show();
}
else
{
txtUserName.BackColor = Color.Red;
txtUserPass.BackColor = Color.Red;
MessageBox.Show(this, "Invalid User Name or Password!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Clear();
txtUserPass.Clear();
txtUserName.Focus();
}
}
finally
{
if ((con != null) && (con.State == ConnectionState.Open))
try { con.Close(); }
catch { /* Do nothing */ }
}
}
image