C# ODBC Connection Exception : "System.NullReferenceException: Object reference not set to an instance of an object"

I am using an OdbcConnection object to query data from my database and i get this Null Reference Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

This is my C# source code:

public void ValidateConnection(string connectionString)
    // i verified the connection string, it works fine.
    OdbcConnection connection = new OdbcConnection(connectionString);
    OdbcCommand command;
    OdbcDataReader reader;
    int qresult;
        command = new OdbcCommand("select id, username, password from users where username=?", connection);
        command.Parameters.Add("username", tb_username.Text);
            reader = command.ExecuteReader();
            if (reader.Read())
                if (reader.GetString(2) == tb_password.Text)
                    // Found the correct password and username match.
                    OdbcCommand command1 = new OdbcCommand("update users set username=?,password=? where username=?", connection);
                    command1.Parameters.Add( "password", tb_password.Text);
                    command.Parameters.Add("username", tb_username.Text);
                    qresult = command1.ExecuteNonQuery();
                    tb_username.Text = String.Empty;
                    tb_password.Text = String.Empty;
                    MessageBox.Show("ERROR: Password and Username do not match.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show("ERROR: Incorrect username and/or password!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        catch (Exception ex)
            MessageBox.Show("ERROR: Unknown error occured.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    } catch (Exception ex) {
        MessageBox.Show("ERROR: Cannot connect to system Database..", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

The exception Occurs at line number #17 :

reader = command.ExecuteReader();

Please help?


The Visual Studio Debugger is your friend use it wisely, check the inner Exception of that exception, it should guide your to the source of your problem.

You are doing it wrong, to assign a value to an SQL Query parameter you’ll need to do it this way:

command = new OdbcCommand("select id, username, password from users where username=@param1", connection);
command.Parameters.Add("@param1", tb_username.Text);

The same thing for these lines as well:

OdbcCommand command1 = new OdbcCommand("update users set username=@username,password=@username where username=@username2", connection);
command1.Parameters.Add("@password", tb_password.Text);
command.Parameters.Add("@username", tb_username.Text);
command.Parameters.Add("@username2", tb_username.Text);

You first define a parameter name inside the query after the “@” symbol then you can apply any value to that parameter using :

command.Parameters.Add("@parameter", "Value");

I believe that’s the reason why you are getting the Null Reference Exception, as the internal Method (Add) cannot find the parameter that you’ve given it, and mostly if you Check your debugger’s Internal Exception at that point, you might find something related to your parameter, such as “Parameter “name” not found…etc”.

hope that helps.

1 Like

Thanks, that solved it, i can’t imagine i was doing it wrong since beginning.