473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Invalid attempt to Read when reader is closed

Hi,

i wrote this code for fetching data from a table using a stored procedure.
The connection definition and opening are put in a class.
The actual read of data occurs in code-behind.
My problem with this code:

Invalid attempt to Read when reader is closed at line: While dtreader.Read()

Thanks for help
Chris

1) the class:
-----------
Public Class myclass
Public Shared Function mydtreader() As SqlDataReader
Dim dreader As SqlDataReader

Try
Using mConnection As New SqlConnection(param.ConnectionString)

Dim mCom As SqlCommand = New SqlCommand("mysp", mConnection)
mCom.CommandType = CommandType.StoredProcedure
mConnection.Open()
dreader = mCom.ExecuteReader
Return dreader
End Using
Catch ex As Exception
Throw
End Try
End Function
end class

2) code-behind:
---------------
Protected Sub pseudoSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles pseudoSubmit.Click
Dim dtreader As SqlDataReader = chat.mydtreader
While dtreader.Read()
......
End While
dtreader.Close()
End Sub
Oct 5 '07 #1
2 6910
Chris,

Your Using statement closes the connection. But a datareader needs the
connection to be open so it can retrieve rows and deliver them to you in your
While dtreader.Read() loop.

You will need to keep the connection open until you are finished with the
datareader. You might want to use:

dreader = mCom.ExecuteReader (CommandBehavior.CloseConnection)

so that when you eventually close the datareader the connection will get
closed.

Kerry Moorman
"Chris" wrote:
Hi,

i wrote this code for fetching data from a table using a stored procedure.
The connection definition and opening are put in a class.
The actual read of data occurs in code-behind.
My problem with this code:

Invalid attempt to Read when reader is closed at line: While dtreader.Read()

Thanks for help
Chris

1) the class:
-----------
Public Class myclass
Public Shared Function mydtreader() As SqlDataReader
Dim dreader As SqlDataReader

Try
Using mConnection As New SqlConnection(param.ConnectionString)

Dim mCom As SqlCommand = New SqlCommand("mysp", mConnection)
mCom.CommandType = CommandType.StoredProcedure
mConnection.Open()
dreader = mCom.ExecuteReader
Return dreader
End Using
Catch ex As Exception
Throw
End Try
End Function
end class

2) code-behind:
---------------
Protected Sub pseudoSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles pseudoSubmit.Click
Dim dtreader As SqlDataReader = chat.mydtreader
While dtreader.Read()
......
End While
dtreader.Close()
End Sub
Oct 5 '07 #2
Thanks

"Kerry Moorman" <Ke**********@discussions.microsoft.comschreef in bericht
news:6C**********************************@microsof t.com...
Chris,

Your Using statement closes the connection. But a datareader needs the
connection to be open so it can retrieve rows and deliver them to you in
your
While dtreader.Read() loop.

You will need to keep the connection open until you are finished with the
datareader. You might want to use:

dreader = mCom.ExecuteReader (CommandBehavior.CloseConnection)

so that when you eventually close the datareader the connection will get
closed.

Kerry Moorman
"Chris" wrote:
>Hi,

i wrote this code for fetching data from a table using a stored
procedure.
The connection definition and opening are put in a class.
The actual read of data occurs in code-behind.
My problem with this code:

Invalid attempt to Read when reader is closed at line: While
dtreader.Read()

Thanks for help
Chris

1) the class:
-----------
Public Class myclass
Public Shared Function mydtreader() As SqlDataReader
Dim dreader As SqlDataReader

Try
Using mConnection As New
SqlConnection(param.ConnectionString)

Dim mCom As SqlCommand = New SqlCommand("mysp",
mConnection)
mCom.CommandType = CommandType.StoredProcedure
mConnection.Open()
dreader = mCom.ExecuteReader
Return dreader
End Using
Catch ex As Exception
Throw
End Try
End Function
end class

2) code-behind:
---------------
Protected Sub pseudoSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles pseudoSubmit.Click
Dim dtreader As SqlDataReader = chat.mydtreader
While dtreader.Read()
......
End While
dtreader.Close()
End Sub

Oct 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
0
by: CW | last post by:
I get this FieldCount error when I attempt to bind a datagrid with a dataset, not a datareader object. The code snippet is as belows: 'PopulateForm called in Page Load event Private Sub...
2
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As...
3
by: Patrick Olurotimi Ige | last post by:
With the code below i get error:- Invalid attempt to read data when reader is closed. //Get a datareader SqlDataReader objDataReader; objDataReader =...
9
by: Tom John | last post by:
Hi I am storing images in an access database, based on an MSDN article. The code i use to store is as follows: <code> 'Create the command object Dim command As New...
4
by: Dave | last post by:
I'm using a datareader to get data from an sql table. The line that gives the error is as follow, dtrReceivers.ToString() which gives the error, Invalid attempt to read when no data is...
1
by: sivam.solai | last post by:
Public Function GetTopLevelThreadName(ByVal ThreadID As Integer) As SqlDataReader Sp_Datasql = "WS_Get_TopLevelThreadName" SqlParam = New SqlParameter(0) {} Try SqlParam(0) = New...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
1
by: mark.norgate | last post by:
Hi I have a problem with a repeater and a data connection. In my Page_Load, I have the following code: Dim connectionString As String = WebConfig.ConnectionString Dim connection As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.