tags:

views:

92

answers:

3

I am working on an ASP.NET 2.0 website. The issue that I'm having is that it queries a database to get the info it displays on screen, but the database occasionally gets to where it has too many open connections. This causes the website to reject the attempt to log-in for anyone, after that database error.

This is caused because many users will log-in, do what they need to do, but then leave the website running while they do other things without logging out. It will time out on them, but the connection still seems to be open. We then have to contact the person in charge of the server it's running on and have him reset it for us.

I have looked and all connections made to the database seem to be closed after the request and query is made. So, what I want to do is to add a button that when clicked will reset the website, instead of having to call the guy in charge of the server every time. Then we can reset it whenever we need to. So, how do I reset an ASP.NET 2.0 website with a button on one of the pages inside the site?

Many thanks,

Mike

+4  A: 

I don't think adding a button to reset the website is the correct choice.

You should really look into why the connections aren't closing.

If you're using SqlConnections, then wrap them in a using statement, this will dispose of the connection after you're finished.

Here's an example:

using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();
   // Do work here; connection closed on following line.
}
Rutger
+6  A: 

all connections made to the database seem to be closed after the request

The problem here is the word "seem". For example, this code "seems" like it will close the connection, but in some situations it won't:

var conn = new SqlConnection("MyConnection");
var cmd = new SqlCommand("query string here", conn);

cmd.ExecuteNonQuery();
conn.Close():

I can hear you saying, "Of course it closes the connection. Don't you see the 'conn.Close();' line?" The problem is that there are things that can happen that prevent the conn.Close() line from executing.

Instead, you need to do something like this:

using (var conn = new SqlConnection("MyConnection"))
using (var cmd = new SqlCommand("query string here", conn))
{
    cmd.ExecuteNonQuery();
}

That code will always close the connection.

If you're really serious about "resetting" the application, you might try calling Environment.Exit(), but again: this is a bad idea.

Joel Coehoorn
A: 

To answer your actual question, the easiest way to reset a ASP.NET site is to just modify the web.config which will cause the site to reload.

So if I wanted to implement a button all I would do is set a value in the app settings that is meaningless (perhaps a date time of the last reset) and then use ConfigurationManager to save the changes.

MSDN reference: ConfigurationManager Class

Chris Marisic
I think the point of this is that he's not allowed to change _anything_ on the site without going through a slow, formal process to get an systems admin involved. He wants to be able to skip all that. I'm not saying it's a good idea, but it's what I think he's after.
Joel Coehoorn
My solution is perfect for that, it lets you create a button that on demand causes your website to restart.
Chris Marisic
WTF for the DV my solution exactly answers the OP's question unlike almost every other solution here.
Chris Marisic