SIGN UP MEMBER LOGIN:    
ARTICLE

Application Level State Management in ASP.NET

Posted by Prashanth Chindam Articles | ASP.NET Programming January 10, 2011
Application Level State Management is used to maintain the state of all the users accessing the webforms present within the website.
Reader Level:
Download Files:
 

Introduction:

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:

    image1.gif

    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.Lock();
        Application["tusers"] = 0;
        Application["ousers"] = 0;
        Application.UnLock();
    }
    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;
        Application.Lock();
        tusers = int.Parse(Application["tusers"].ToString())+1;
        ousers = int.Parse(Application["ousers"].ToString())+1;
        Application["tusers"] = tusers;
        Application["ousers"] = ousers;
        Application.UnLock();
    }

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

</script>

Code:

using System;

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

Output:

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

image2.gif

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

image3.gif

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

image4.gif

Observation:

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.
  

Login to add your contents and source code to this article
share this article :
post comment
 

I am happy that my article is helpful to U ~ Prashanth Chindam

Posted by Prashanth Chindam Jun 21, 2011

Very helpful:-) Thums up sir:-)

Posted by surender bhyan Jun 20, 2011

thanks for Ur feedback

Posted by Prashanth Chindam Jan 20, 2011

thanx prashanth for this useful tutorial

Posted by vishal shrivastav Jan 20, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor