Solved

Invalid attempt to read when reader is closed

Posted on 2004-08-23
5
780 Views
Last Modified: 2012-05-05
A colleague of mine is experiencing a very perplexing error.

He is using the following code to databind a datagrid:

Dim dbSearchReader As SqlDataReader =            dbConnection.ConnectionString = ConfigurationSettings.AppSettings("Connection")
dbConnection.Open()
dbCommand = New SqlCommand(Session("SQL"), dbConnection)
dbSearchReader = dbCommand.ExecuteReader
dgProducts.DataSource = dbSearchReader
dgProducts.DataBind()

When he runs his program on localhost and the development server the datagrid binds correctly.

When he runs his program on the live server he gets the following error:

System.InvalidOperationException: Invalid attempt to Read when reader is closed. at System.Data.SqlClient.SqlDataReader.Read() at System.Data.Common.DbEnumerator.MoveNext() at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.BaseDataList.DataBind()

Any ideas are appreciated :-)

Thanks,
Dave.

0
Comment
Question by:tidydave
5 Comments
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11872368
You cannot link to a DataReader, a DataReader is only used to display foward only information.


Use a DataSet Instead.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11872526
>> Dim dbSearchReader As SqlDataReader =    ...??
I don't think you pasted the correct code, can you paste it again?
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 11873511
Hi there,
when you bind  ( dgProducts.DataBind()),  ADO.Net closes the Reader. So, the reason you are getting error is, may be, due to something else you are doing after DataBinding. something else is trying to access the Reader.
0
 

Author Comment

by:tidydave
ID: 11878845
Not sure why it put an "=" after the first line of pasted code?  Shall post again.

Dim dbSearchReader As SqlDataReader
dbConnection.ConnectionString = ConfigurationSettings.AppSettings("Connection")
dbConnection.Open()
dbCommand = New SqlCommand(Session("SQL"), dbConnection)
dbSearchReader = dbCommand.ExecuteReader
dgProducts.DataSource = dbSearchReader
dgProducts.DataBind()

Cheers for the comments so far.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11880223
What are you doing right after that ?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

21 Experts available now in Live!

Get 1:1 Help Now