Application Level State Management in ASP.NET


Application Level State Management is used to maintain the state of all the users accessing the webforms present within the website.

The value assigned for an application is considered as an object.

Application object will not have any default expiration period.

Whenever the webserver has been restarted or stopped then the information maintained by the application object will be lost.

If any data is stored on the application object then that information will be shared upon all the users accessing the webserver.

Since the information is shared among all the users, it is advisable to lock and unlock the application object as per requirement.

Global Application Class(Global.asax):

It is a Class which consists of event handlers which executes the code implicitly whenever a relevant task has been performed on the web server.

A Sample program on Application Level State Management:

  • Design:


    Design the form as shown above with two textboxes and one button.
  • Right click on application in solution explorer-> Add new item-> select 'GlobalApplicationClass' file and write the following code in it.

Global.asax code:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
        // Code that runs on application startup
        Application["tusers"] = 0;
        Application["ousers"] = 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
        int tusers, ousers;
        tusers = int.Parse(Application["tusers"].ToString())+1;
        ousers = int.Parse(Application["ousers"].ToString())+1;
        Application["tusers"] = tusers;
        Application["ousers"] = ousers;

    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.
        int ousers;
        ousers = int.Parse(Application["ousers"].ToString()) - 1;
        Application["ousers"] = ousers;



using System;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        string tusers, ousers;
        tusers = Application["tusers"].ToString();
        TextBox1.Text = tusers;
        TextBox2.Text = ousers;
    protected void BtnLogOut_Click(object sender, EventArgs e)


Execute the application, copy the url and open the same url in other browsers. The result will be as shown below:


Now, logout and close one browser and refresh the remaining browsers. The result will be as shown below:


Now, logout and close another browser and resfresh the remaining browser. The result will be as shown below:



We can observe that the state of all the users accessing the website i.e, Total Users and Online Users is maintained by this application.

Similar Articles