Wednesday, December 3, 2008

How to find visitors online using asp.net

Hey! have you ever tried to put the number of visitors online on your aspx page?
Ya.Try this-
Just paste the follwing code in global.asax file:

void Application_Start(object Sender, EventArgs E)
{
// Set our user count to 0 when we start the server
Application["ActiveUsers"] = 0;

}

void Session_Start(object Sender, EventArgs E)
{

Session["Start"] = DateTime.Now;
Session.Timeout = 1;
Application.Lock();
Application["ActiveUsers"] = (int)Application["ActiveUsers"] + 1;
Application.UnLock();

}

void Session_End(object Sender, EventArgs E)
{
Application.Lock();
Application["ActiveUsers"] = (int)Application["ActiveUsers"] - 1;
Application.UnLock();
Session.Clear();
Session.Remove("Start");
}

And get the Application["ActiveUsers"] where ever you need it!!!
Ex:label1.text=Application["ActiveUsers"].ToString();

No comments: