im unable to validate a login of users ,since if im entering the wrong values my datareader is not getting executed y ?

Posted by Salman_Khan on Stack Overflow See other posts from Stack Overflow or by Salman_Khan
Published on 2011-01-01T17:30:06Z Indexed on 2011/01/01 17:54 UTC
Read the original article Hit count: 344

Filed under:
|
|
|
|

//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();
        }
    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about sql