Help: Function that returns DataReader

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

I have a function that returns a datareader. The problem is that in the
function's FINALLY I close the datareader and set it to nothing. But,
BEFORE the Finally I "Return" the datareader (in the Try).

The problem is, by the time it's "returned" it says I already closed it.
Any ideas how I can overcome this?

Example:

Private Sub x
Dim dr as SqlDataReader
....
....
dr = GetDr(...) ' FAILS BECAUSE THE DATAREADER IS CLOSED BY NOW!
....
End Sub

Private Function GetDr(...) as SqlDataReader
Dim MyDataReader as SqlDataReader
Try
.....
....
Return MyDataReader
Catch
Finally
MyDataReader.Close
MyDataReader = Nothing
End Function
 
I think you want to use a dataset, not a datareader. It's
better for passing around between different layers.

I've never tried returning a datareader from a function,
but IF it is possible, then it makes sense then that you
shouldn't close it. Thus, try removing the the Close
method and see what happens. But again, I think you are
going to want to use the dataset, since it's truly
disconnected.
 
Back
Top