-1

My problem is always in throw. please help me to find the problem. thanks!

MySqlConnection cnn = new MySqlConnection(mysqladdress);

cnn.Open();

try
{
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = cnn;
    cmd.CommandText = "SELECT * FROM info where StudentID ='" + textBox1.Text + "'and Name='" + textBox2.Text + "'";
    MySqlDataReader reader = cmd.ExecuteReader();

    int count = 0;
    while (reader.Read())
    {
        count = count + 1;
    }

    if (count == 1)
    {
        MessageBox.Show("Welcome");
    }
    else if (count > 1)
    {
        MessageBox.Show("Access Denied");
    }
    else
    {
        MessageBox.Show("Wrong student ID and Password");
    }

}
catch (Exception)
{
    throw;
}
jrbedard
  • 3,336
  • 5
  • 26
  • 34

1 Answers1

2

This must be a typo or something simple which is hard to tell without knowing the exact error message.

To avoid such errors i would recommend you to use parameters:

cmd.CommandText = "SELECT * FROM info where StudentID = @StudentID and Name= @Name";
cmd.Parameters.Add("@StudentID", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = textBox2.Text;
fubo
  • 39,783
  • 16
  • 92
  • 127