Invalid attempt to Read when reader is closed.

  • Thread starter Thread starter sivam.solai
  • Start date Start date
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
 
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
 
Back
Top