Solved

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

Posted on 2007-11-16
5
293 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:joex911
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:
joex911 earned 50 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:joex911
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Combine two columns 3 37
DLL in ASP.NET 20 41
Server error in 'XXXXXXXX' application. 12 49
creating a flowchart from an algorithm 5 21
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now