im unable to validate a login of users ,since if im entering the wrong values my datareader is not getting executed y ?
- by Salman_Khan
//code
private void glassButton1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox1.Text == "" || comboBox1.SelectedIndex == 0)
{
Message m = new Message();
m.ShowDialog();
}
else
{
try
{
con.ConnectionString = "Data source=BLACK-PEARL;Initial Catalog=LIFELINE ;User id =sa; password=143";
con.Open();
SqlCommand cmd = new SqlCommand("Select LoginID,Password,Department from Login where LoginID=@loginID and Password=@Password and Department=@Department", con);
cmd.Parameters.Add(new SqlParameter("@loginID", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
cmd.Parameters.Add(new SqlParameter("@Department", comboBox1.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string Strname = dr[0].ToString();
string StrPass = dr[1].ToString();
string StrDept = dr[2].ToString();
if(dr[2].ToString().Equals(comboBox1.Text)&&dr[0].ToString().Equals(textBox1.Text)&&dr[1].ToString().Equals(textBox2.Text))
{
MessageBox.Show("Welcome");
}
else
{
MessageBox.Show("Please Enter correct details");
}
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex);
}
finally
{
con.Close();
}
}
}