Every website needs counter to keep track of visitors. If you are implementing a hit counter always remember to start the count from 10,000 :) just kidding. You can easily make a hit counter by using a Application variable and increase its count in Application_BeginRequest or Session_Start events for every user visit. The problem is that once the server restarts your application level variables will be reinitialized and hence they will loose all count.
A good way is to store the visitor count in the database. You must have some maximum count which when reached pushes the application variable into the database. This will save accessing the database on each request.
Here is a little code snippet from my counter. You can always merge "Visit" and "GetTotalCount" method as save one extra database access. The complete code will be explained in the article. Here is the link to the article
Implementing Hit Counter in Asp.net
protected void Session_Start(Object sender, EventArgs e)
{
Application[TOTALUSERS] = Convert.ToInt32(Application[TOTALUSERS]) + 1;
// If you need a running count should declare another Application variable
Application[RUNNINGCOUNT] = Convert.ToInt32(Application[RUNNINGCOUNT]) + 1;
if((int)(Application[TOTALUSERS]) >= Convert.ToInt32((ConfigurationSettings.AppSettings["MAX"])) )
{
Visit.Count((int) Application[TOTALUSERS]);
// Initialize the count again
Application[TOTALUSERS] = 0;
// Retrieve the count from the database
Application[RUNNINGCOUNT] = Visit.GetTotalCount();
}
}