I have an issue where I'm trying to create a login form, however the else statement seems to be ignored.
How can I write this code extract so that a message box is shown when the incorrect data is put into the text boxes? (All databases are set up correctly).
try
{
sc.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from StudentRecords where ID = '" + txtBoxUsername.Text + "' ", sc); //where ID = '" + txtBoxUsername.Text + "' and DOB = '" + textBoxPassword.Text + "'
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if (txtBoxUsername.Text == (myReader["ID"].ToString()) && textBoxPassword.Text == (myReader["DOB"].ToString()))
{
LoginSuccessForm loginfrm = new LoginSuccessForm();
loginfrm.Show();
this.Hide();
}
else if (txtBoxUsername.Text != (myReader["ID"].ToString()) || textBoxPassword.Text != (myReader["DOB"].ToString()))
{
MessageBox.Show("Incorrect Password and/or Username", "Error");
break;
}
}
sc.Close();
}
I have tried putting the messagebox outside of the while loop and that doesn't work in the desired way. (Following the try method is a catch, I didn't include it to save space).
In saying that, it seems to only pick up the first user in the database too. Any clues or guidance would be appreciated!
else if (txt...with a simpleelse. The check in the if is redundant.