I'm practising with C# and SQL and I'm trying to make a simple email
+ password
login that checks the database to match the input.
Why does this code return the expected 0
when the input is wrong, but says the input string is wrong when correct data is used?
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText =
"SELECT UserPassword, UserMail FROM Users WHERE UserPassword = '" +
textBox2.Text +
"' AND UserMail = '" +
textBox1.Text + '\'';
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
int correct = 0;
correct = Convert.ToInt32(cmd.ExecuteScalar());
sqlConnection1.Close();
if(correct <= 0)
{
MessageBox.Show("Wrong input. Correct = " +
Convert.ToString(correct) +
"\n" +
cmd.CommandText);
}
textbox1
and textbox2
are email and password inputs, respectively.
I expected the output of "email5" + "email5" to be 5, since it was the fifth row (same with other valid data), but I got the following exception:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.