0

I've implemented the following code multiple times, but this time it gives me the error System.NullReferenceException: The object reference was not set to an object instance

Here is the code:

    bool userExists = false;
    string existCheck = "SELECT COUNT(*) FROM tablename WHERE name = @name;";
    MySqlCommand cmd = new MySqlCommand(existCheck, connect);
    cmd.Parameters.AddWithValue("@name", username);
    int userCount = Convert.ToInt32(cmd.ExecuteScalar()); <- The error is produced here
    if (userCount > 0)
            {
                userExists = true;

            }

In another part of the project where I used the same code with other variables it works without problems.

Greetings

Loli
  • 1
  • 3
  • Split that line in its parts. Assign the result of ExecuteScalar to an object variable and check with the debugger what it returns. Strange, it should not be null. – Steve Jan 08 '21 at 11:43
  • Can you post the exception with stacktrace please? – Glubus Jan 08 '21 at 11:51
  • Hm, if the connection is valid, the result of `ExecuteScalar()` should not be null. If the connection is not valid, this should throw a different exception ... – derpirscher Jan 08 '21 at 12:25
  • [MySqlCommand.ExecuteScalar](https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlCommand_ExecuteScalar.htm) returns _"The first column of the first row in the result set, **or a null reference if the result set is empty** "_ (emphasis by me) - Not sure if the query should return a 0 or empty result set, though. I'd have expected a 0. But who knows ... Maybe issue the query in a MySql GUI to see what it is supposed to return. – Fildor Jan 08 '21 at 12:48
  • If you have an index on `name`, could you give this a try: `SELECT EXISTS(SELECT 1 FROM tablename WHERE name = @name LIMIT 1)` ? – Fildor Jan 08 '21 at 12:57
  • @Steve It shows "Null" in the debugger, but the error still pops up – Loli Jan 08 '21 at 13:37
  • @Fildor I did it in the Ubuntu-Console and the query works fine – Loli Jan 08 '21 at 13:40
  • _"It shows "Null" in the debugger,"_ - where, when and for what? Can you post a screenshot of that? – Fildor Jan 08 '21 at 13:42
  • @Glubus System.NullReferenceException HResult=0x80004003 Message= Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. Source = MySql.Data Stacktrace: at MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar() – Loli Jan 08 '21 at 13:45
  • Just to hook that off the list: `username` is actually not-null, right? – Fildor Jan 08 '21 at 13:51
  • @Fildor username is a variable taken from input by the user, so it depends. – Loli Jan 08 '21 at 13:55
  • It’s actually not ‚null‘, but the number 0. – Loli Jan 08 '21 at 13:56
  • _Never_ trust user input. The first thing you do is check `string.IsNullOrWhitespace(username)` :) – Fildor Jan 08 '21 at 13:57
  • The number 0? `username` is of type `string`, is it? So `"0"` would be legal input, but I am not sure if you have to force type string when putting it into params, since it's a numerical-only string. – Fildor Jan 08 '21 at 14:00

0 Answers0