When a site runs then how we can find that in a single time, how many users are using the site? In this article I am going to show how we can trap that how many users are hitting the site.
For this I used Global.asax. In Global.asax we find many method such as Application_Start, Application_End, Session_Start, Session_End etc.
I used here Application_Start and Session_Start method.
In Application_Start method I declare a varaible of Aplication Type like as:
void Application_Start(object sender, EventArgs e)
{
Application["User"] = 0;
}
After declaring it, I used Session_Start Method like as:
void Session_Start(object sender, EventArgs e)
{
int count;
count = (int)Application["User"];
count = count + 1;
Application["User"]=count;
}
In this Session_Start method I declare a variable of integer type. I type cast the Application["User"] with int to store the value of Application["User"] in variable count. On running application a new session create. So when new session will create then this Session_start method will call. On every call the value of variable count will increase by 1. With this we can trap the total no. of users, which are hitting the site at a same time.
The number of total users will come on form. For this I do the code on Page_Load like as:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Application["User"].ToString());
}