i want online user and total visitor to my site i take two label to show online user and total user.and write below code in global.asax
and for load .but it always show output online user 1 that is correct but total user also 1.so i want when agai i run my application it will show total user2,3,4 so on
lbl_onlineuser.Text = Application["onlineuser"].ToString();
lbl_totaluser.Text = Application["Totaluser"].ToString();
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["Totaluser"] = 0;
Application["onlineuser"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session.Timeout = 20;
Session["Start"] = "Now";
Application.Lock();
Application["Totaluser"] = Convert.ToInt32(Application["Totaluser"]) + 1;
Application["onlineuser"] = Convert.ToInt32(Application["onlineuser"]) + 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["onlineuser"] = Convert.ToInt32(Application["onlineuser"]) - 1;
Application.UnLock();
Loading

Sunny SharmaPosted May 24, 2013, 5:07 AM
At the very first I would say, your application is working absolutely fine.
The problem why your application gets reset every time you close the browser is you'ew using ASP.NET Development Server for testing. As soon you deploy this application on IIS Server it will go. No problem you see there. The Application variables will be maintained by IIS Server Application Pool.
Also, here as an alternative, I've created a sample project for you. please go through.This resolves the issue of application variable.
Thanks.
Bhabani prasad DashPosted May 24, 2013, 6:02 AM
Bhabani prasad DashPosted May 24, 2013, 4:09 AM
Sunny SharmaPosted May 24, 2013, 3:51 AM
I don't see any problem with your code. Seems you're trying to open the page in different tabs in the same browser. One browser opens a session only one time hence the Session_Start() will not be invoked on your second request to from the same browser.
Open the page in any other browser and your count will increase.
Hope this helps!
Don't forget to accept this as answer if it helps!
Thanks.