Solved

Invalid attempt to read when reader is closed

Posted on 2004-08-23
5
795 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
[X]
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
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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