I am using:
string selectString =
"SELECT username, password " +
"FROM users " +
"WHERE username = '" + user + "' AND password = '" + password + "'";
MySqlCommand mySqlCommand = new MySqlCommand(selectString, Program.mySqlConnection);
Program.mySqlConnection.Open();
String strResult = String.Empty;
strResult = (String)mySqlCommand.ExecuteScalar();
Program.mySqlConnection.Close();
if (strResult.Length == 0)
{
responseString = "invalid";
InvalidLogin = true;
} else {
InvalidLogin = false;
}
and at strResult.Length I get a NullReferenceException for some reason.
This is how your code should look like:
There are few things to notice
injection” to find more about what could happen to you. ALWAYS use
parameters. I know, you wrote that this is console app, but good habits are important.
usingkeyword when working with databaseconnections and commands.
To answer your question, the problem is in ExecuteScalar that you are using. It returns scalar variable (single value)…in your query, you are returning username and password, so you should use ExecuteReader instead…but I think that COUNT(*) in the query that I posted, together with ExecuteScalar might be a better solution.