Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Invalid attempt to read when reader is closed

Posted on 2004-08-23
5
Medium Priority
?
802 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 1500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

885 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