Solved

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

Posted on 2007-11-16
5
295 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

8 Experts available now in Live!

Get 1:1 Help Now