Count current logged user in asp.net
Count current logged user in asp.net
In this article I am writing how to know number of current logged
or working user in application.
We can achieve this using session value generate at logging time.
The base of this process is how much different session value is working on application
at current time. Then we count it using Global file logic.
Step 1: Design web form .aspx page
<div>
<asp:Label ID="user_count" runat="server"></asp:Label>
</div>
</form>
Step 2:Logic on .cs Page
//for testing you
have to change session value and run it on different browser.
//whenever it find a new session value it automatically
increase its count by 1.
Session["name"] = "sam";
user_count.Text = "Count Login User: " +Convert.ToString(Application["usercount"]);
Step 3: Add a Global.asax file in your application.
Right click on solutiuon > add new item > add
Global.asax file
Writw this simple code on Global.asax
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code
that runs on application startup
Application["usercount"] = "0";
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Application["usercount"] = "0";
}
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
Application["usercount"] = Convert.ToInt32(Application["usercount"]) + 1;
}
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["usercount"] = Convert.ToInt32(Application["usercount"]) - 1;
}
</script>
Comments
Post a Comment