Invalid attempt to Read when reader is closed.

S

sivam.solai

Public Function GetTopLevelThreadName(ByVal ThreadID As Integer) As
SqlDataReader

Sp_Datasql = "WS_Get_TopLevelThreadName"
SqlParam = New SqlParameter(0) {}

Try

SqlParam(0) = New SqlParameter("@ThreadID", SqlDbType.Int)
SqlParam(0).Value = ThreadID

MyConnnection = GetConnection()
MyReader = SqlHelper.ExecuteReader(MyConnnection,
CommandType.StoredProcedure, Sp_Datasql, SqlParam)


Catch ex As Exception

Finally
If Not MyConnnection Is Nothing Then
CType(MyConnnection, IDisposable).Dispose()
End If
End Try

Return MyReader
End Function

i call above Function in Thread.aspx File code is below

MyReader = MyForum.GetTopLevelThreadName(thisThreadId)
Public MyReader As SqlDataReader

While MyReader.Read()
threadLink.NavigateUrl = "topicView.aspx?id=" &
MyReader("psRelBcId")
threadLink.Text =
myIncludes.getTopicName(MyReader("psRelBcId"))
topThreadSubject.Text = MyReader("psSubject")
End While

this use above code i got Error
Invalid attempt to Read when reader is closed.

pls advise me
 
T

Teemu Keiski

Hi,

you cannot close connection in " GetTopLevelThreadName" in Finally block


" Finally
If Not MyConnnection Is Nothing Then
CType(MyConnnection, IDisposable).Dispose()
End If
End Try
"

as it closes it before the calling code gets access to the reader. Create
the reader with CloseConnection (passed to ExecuteReader) and make sure
calling code calls Close for the reader instance. That way connection will
be closed.

Or you can use technique I've demonstrated in my article:

Using Delegates with Data Readers to Control DAL Responsibility
http://aspalliance.com/526
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top