Calculate Online users and Total Users in ASP.net
In this article I'm trying to explain how to calculate total Users and online users using global.asax file. Here we give the count of users and in page wise we give the session timeout. By default timout for session is 20 min, we replace that with 1 min in programmatically.
Online Users & Total Users Count:
An ASP.net Application is automatically started by IIS. When first user makes a request for resources. Application shutdown will happen when all the users leave the Application & Application idle time out expire.
Session is started Automatically by ASP.net for every new user. Every session is provided with session Id which is internally a COOKIE created by ASP.net. Session Id will travel between every request & response. A session is enabled by ASP.net based on idle timeout. By default session idle timeout is 20min. which means if user doesn't request for server for 20min, then server will destroy the session. we can change this time in web.config as well as programmatically using session object.
When Application starts…..?
Application started automatically when first user make a request
When Session starts….?
When new user make a request session will starts.
When Pool starts…?
When IIS will starts pool started.
When pool ends…?
When IIS will end pool ended.
When Session ends….?
Idle time 20 min's session will end.
When Application ends…?
All sessions are ended then Application end.global.asax:
If global.asax is not available in project then add that by right click on project section on Add NewItem and choose global.asax file, In a project only one global.asax file only available. So, we just keep that and wrote below lines of code under global.asax file.
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["TotalUsers"] = 0;
Application["OnlineUsers"] = 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
Application["TotalUsers"] = Convert.ToInt32(Application["TotalUsers"]) + 1;
Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) + 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["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) - 1;
}
</script>Source Code:
After wrote a code in global.asax then right click on project section then add new webpage and give a name for that as status.aspx and design a page like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="status.aspx.cs" Inherits="status" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
font-family: "Trebuchet MS";
font-size: x-large;
color: #000099;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<span class="style1"><strong>Online Statistics</strong></span><br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
</div>
</form>
</body>
</html>Code Behind:
After design a page wrote below lines of code in code behind section.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class status : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//programatically set a timeout for session
Session.Timeout = 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Total Users: " + Application["TotalUsers"].ToString();
Label2.Text = "Online Users:" + Application["OnlineUsers"].ToString();
}
}Steps to execute:
put a break point in global.asax page Application startup and session startup and session end. Then see the difference when application start and when session start and when session end.
1) When first user make a request then Application starts.
2) When new user make a request then session starts
3) after that it's showing the output page.
4) After click on button the count will fetch from application ans session event from global.asax
5) When idle time is complete then session is completed, here my session idle time is 1 min. now the online users count is 2 after session end the count should be decreased by 1 and now the application count is 1.OutPut:
Every 1 minute i try to open new browser and every time Total Users count has been incremented at the same time Online Users count has been decremented by 1 and again new request is raised so again Onliine Count has been incremented by 1.
This is the final output. Conclusion:
Using this we can easily calculate the Total User Count and Online User count information.