asp.net datareader error -Object variable or With block variable not set.

The below code returns  Object variable or With block variable not set.
at  
dgResults.DataSource = objDataReader

What does this mean?


code.....
 ' Set up our connection.
                Dim objConnection As SqlConnection
                objConnection = New SqlConnection("Server=localhost;Database=customers;UID=sa;PWD=go;")
                objConnection.Open()

                'Specify the SQL string
                Dim strSQL As String = "Select First_Name,Last_Name from USERS_TBL WHERE ID=" & strUserID2 & ""
                'Create a command object
                Dim objCommand As SqlCommand
                objCommand = New SqlCommand(strSQL, objConnection)

                objCommand.ExecuteNonQuery()
                'Get a datareader
                objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

                'Do the DataBinding
                dgResults.DataSource = objDataReader
                dgResults.DataBind()
                getFirst = objDataReader("FIRST_NAME")
                getlast = objDataReader("LAST_NAME")
   
                getuserinfo = getlast & "," & getFirst
                ' Close the datareader/db connection
                objDataReader.Close()
cseinkAsked:
Who is Participating?
 
Oliver AmayaConnect With a Mentor EntrepeneurCommented:
Try this instead of the DataReader:
Dim adapter As SqlDataAdapter = new SqlDataAdapter()
adapter.TableMappings.Add("Table", "USERS_TBL")
adapter.SelectCommand = cmdSuppliers
Dim ds As DataSet = New DataSet("Users")
adapter.Fill(ds)
 
dgResults.DataSource = ds
dgResults.DataBind()

Open in new window

0
 
Oliver AmayaEntrepeneurCommented:
Hi, remove this line:

objCommand.ExecuteNonQuery()

Open in new window

0
 
cseinkAuthor Commented:
The error is still showing up and has something to do with
dgResults.DataSource = objDataReader
0
 
Oliver AmayaEntrepeneurCommented:

'This
adapter.SelectCommand = cmdSuppliers
 
'Should be
adapter.SelectCommand = objCommand

Open in new window

0
 
cseinkAuthor Commented:
This got it. Thanks
0
All Courses

From novice to tech pro — start learning today.