Happy .NET Happy .NET - 21 days ago 6
ASP.NET (C#) Question

how to count number of visitors for website in asp.net c#

How do I count the number of visitors for website in asp.net c#?

I am using the code below:

in global.asax page

<%@ Application Language="C#" %>




void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfVisitors"] = 0;
}





void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}




in .aspx page

<asp:Label runat="server" ID="lbluser" />


in .aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
lbluser.Text = Application["NoOfVisitors"].ToString();
}


The application counter is resetting to 0 every one hour ...
Where have I erred in counting the number of users? ...thank u...

Answer

If your application is hosted in IIS and has an application pool, you can check the Application Pool Recycling Settings. Depending on your version, the default is 1740 or 29 hours. Maybe the pool for your application is configured to 60 or around that value? The next setting to check is the Idle Time Out. I believe its default value is 20 on a new server. You can set this to 0. I recommend you read about these settings prior to changing them.