Solved

Invalid attempt to read when reader is closed

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 71
Delete HTML table rows 12 29
SignalR's 'connection.Hub' only returns the Hub on the first page 1 27
imap read mail 1 29
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

803 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