Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last event on page? 1

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I am new to ASP.NET. I do some database queries in my script and want to close the connection at the end of the script. Where should I put the disconnect? Maybe I don't know when the last execution will be and there will be several queries so I want to disconnect at the "last stage". Any ideas?

--- neteject.com - Internet Solutions ---
 
I wouldn't really recommend closing your connections in the unload sub of the page as you shouldn't leave connections open.

You should only have your connection open whilst running the actual SQL command so I would suggest placing your code in a sub/function (and call this each time you need to run any SQL) and using a Try/Catch. e.g
Code:
    Private Sub RunMySQL(ByVal strSQL As String)
        Try
            ' Open Connection and run SQL

        Catch ex As Exception
            ' Catch any exceptions

        Finally
            ' Clean Up and close connections

        End Try
    End Sub


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Regarding connections...it is better to open late and close early. I usually use the method mentioned by Ca8msm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top