Solved

Invalid attempt to read when reader is closed

Posted on 2004-08-23
5
793 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

Independent Software Vendors: 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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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