?
Solved

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

Posted on 2007-11-16
5
Medium Priority
?
301 Views
Last Modified: 2012-06-27
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()
0
Comment
Question by:cseink
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20299837
Hi, remove this line:

objCommand.ExecuteNonQuery()

Open in new window

0
 

Author Comment

by:cseink
ID: 20299886
The error is still showing up and has something to do with
dgResults.DataSource = objDataReader
0
 
LVL 10

Accepted Solution

by:
Oliver Amaya earned 200 total points
ID: 20300144
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
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20300152

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

Open in new window

0
 

Author Closing Comment

by:cseink
ID: 31409592
This got it. Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question