To do this,
1) Add a new item global.asax in your website.
2) Put the following code in it
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application[“UsersOnline”] = 0;
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application[“UsersOnline”] = (int)Application[“UsersOnline”] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application.Lock();
Application[“UsersOnline”] = (int)Application[“UsersOnline”] – 1;
Application.UnLock();
}
3) In the webpage where the no of online users have to be displayed make use of this application object
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(“The no of users online are ” + Application[“UsersOnline”].ToString());
}
That’s it
I have used this code.. this works fine on local system but on live site it some sometimes shows negitive number. i couldn’t understand why it is so
LikeLike
Works great
Thank you
LikeLike
I am using this way i got it Thanks for ur post
LikeLike
really nice solution…..
Thank you…..
LikeLike
Its working fine .But application session couldn’t reset when the application met abnormal end.
Thanks in advance
LikeLike
Hy
LikeLike
its giving me following error while using in page load event
lblUsersOn.Text = Application[“UsersOnline”].ToString();
Object reference not set to an instance of an object.
LikeLike