Calculate Online users and Total Users in

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 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 for every new user. Every session is provided with session Id which is internally a COOKIE created by Session Id will travel between every request & response. A session is enabled by 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.


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;


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" "">

<html xmlns="">
<head runat="server">
<style type="text/css">
font-family: "Trebuchet MS";
font-size: x-large;
color: #000099;
<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 />


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.

App Start

2) When new user make a request then session starts

Session start

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.



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.



Using this we can easily calculate the Total User Count and Online User count information.


  • Source Code (
  • Comments

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: