Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jun 18, 2013, 8:22 PM EDT
The sample code is given below:
void Application_OnStart(Object Sender, EventArgs E)
{
Application["CurrentUsers"] = 0;
}
void Session_OnStart(object Sender, EventArgs E)
{
Application.Lock();
Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
Application.UnLock();
}
void Session_OnEnd(object Sender, EventArgs E)
{
Application.Lock();
Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
Application.UnLock();
}
or you can follow below link
http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx
http://www.sujitbhujbal.com/2012/12/how-to-count-website-visitors-in-aspnet.html
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 18, 2013, 11:06 PM EDT
My suggestion is to use two variables to track counts, one for Online visitors and other for the Total.
Edit your Global.asax file for Session_Start and Session_End events as below:
----------------------------------------------------------------------------
public static int totalVisitors,onlineCounter = 0; void Session_Start(object sender, EventArgs e)
{
Application.Lock();
onlineCounter++;
totalVisitors++;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
onlineCounter--;
Application.UnLock();
}
-----------------------------------------------------------------------------
I prefer using an static variable because:-
"It is recommended that you store data in static members of the application class
instead of in the Application object. This increases performance because you can
access a static variable faster than you can access an item in the Application
dictionary." -says Microsoft (http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607)
Hope you got the point.
Mark this as answer if it helps.
Cheers!
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Jun 18, 2013, 8:09 PM EDT
Please go through below link
http://imar.spaanjaars.com/223/howto-create-a-hit-counter-using-the-globalasax-file-in-aspnet-1x
I hope it will be helpful.
Pankaj PandeyRank #642 · 3.7K reputationPosted Jun 18, 2013, 8:41 PM EDT
http://aspdotnetby.blogspot.in/2012/04/site-counter-in-aspnet-and-c.html
sourav mondal 049 reputationPosted Oct 25, 2013, 9:39 AM EDT