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
Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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