Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-11-16
5
Medium Priority
?
302 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
  • 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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

810 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