If a user clicks the X to close my web page, I want to destroy a database connection. How do I capture that event?
Printable View
If a user clicks the X to close my web page, I want to destroy a database connection. How do I capture that event?
Why do you have the database connection open for so long? If you need some things from the database to render on a page, do it in the page load event and get the data. Then close the connection when you are done getting the data. Then on post backs you can reopen the connection if you need to.
You are not going to know when the user exits the browser, the only thing you can really do is store the connection in the session object, but that won't go away until after a certain amount of time has passed where the user no longer is accessing the server.
Not to mention that .Net will probably have closed it anyway. I always access data in asp.net like the following:-
try
conn.open
dr = cmd.executereader
'whatever else
finally
conn.close
dr.close
end try
This will not raise an error if the objects are already closed like it might have done in asp.
Also I'm using a datareader and yes in asp.net thats all I ever use. Why people have jumped at datasets and the dataadapter for webpages I'll never know.