Object reference not set to an instance of an object?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

Can you look at my code and tell me what I did wrong?

I get this error when I try to execute this line of code:
VB.NET:
            objDataAdapter.SelectCommand = New OleDbCommand(strSqlStatement, objConnection)

Here's the error I get:
VB.NET:
Object reference not set to an instance of an object.

Here's the surrounding code so you know what we are trying to do:
VB.NET:
    Private Sub PopulateClassNameComboBox()
        ' Declare strings to hold SQL Statements.
        '----------------------------------------
        strSqlStatement = _
            "SELECT ClassName,  " & _
                   "ClassID " & _
              "FROM Class " & _
              "ORDER BY 1"

        ' Create the connection object to use an SQL query.
        '--------------------------------------------------
        objConnection = New OleDbConnection(My.Settings.ISGLConnectionString)

        ' Set up a command to execute a query.
        '-------------------------------------
        objCommand = New OleDbCommand(strSqlStatement, objConnection)
        objCommand.CommandType = CommandType.Text

        ' Load the data into the DataAdapter then into the ComboBox.
        '-----------------------------------------------------------
        Try
            objDataAdapter.SelectCommand = New OleDbCommand(strSqlStatement, objConnection)

            objDataAdapter.Fill(objDataSet, "Class Names")

            With ComboBoxClassName
                .DataSource = objDataSet.Tables("Class Names")
                .DisplayMember = "ClassName"
                .ValueMember = "ClassID"
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

Thanks.

Truly,
Emad
 
Check the objDataAdapter because I'm not seeing a "New" line in your code for it. I see one for your objConnection object though.
 
Back
Top