By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Closing database connection.

P: n/a
I wrote a simple class to supply a datareader object (code at bottom of
post) back to a page. I access it on the APSX page like this:

Dim myDR as New getDataReader(strSQL)

If myDR.ResultFailed = True Then

MessageText.InnerHTML = "Internal error."

dataList.DataSource = myDR.Result

End If

From time to time it get weird errors, which I tend to think stem from not
closing the database connection in the class code. Yet, if I explicitly
close the connection, I get an error that says "Invalid attempt to
FieldCount when reader is closed. " I've commented-out the
connection-closing code below, and this code actually works. Is there any
way I can make sure the connection gets closed in hopes of preventing the
other random errors?

Thanks for any help!


Public Class getDataReader

Public Result AS OLEDBDatareader
Public ResultFailed as Boolean = True

Public Sub New(strSQL AS String)

Dim myConn as new OleDBConnection (connString)

Dim myCommand AS new OleDBCommand(strSQL, myConn)

Result = myCommand.ExecuteReader()
ResultFailed = False
myConn = Nothing

ResultFailed = True
End Try

End Sub

End Class
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.