Help: Function that returns DataReader

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
 
R

Ryan Jacops

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.
 

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

Top