Application state is used to store data on the application machine. It works as a global variable in other programming languages. Application variable is an object that is shared by the multiple sessions. We can use application variable within page, HttpHandler and Global.asax.

When request a page from client machine, an instance will create at application machine by the help of HttpApplicationState class. Entire application will have only one instance which is provided by HttpContext property named Application.

How to create Application state?

It's very easy to create application state in the ASP.NET application. I am going to take you to write simple application state program now.

Example 1: In the Global.asax page

void Application_Start(object sender, EventArgs e)

{

// Code that runs on application startup

Application["LoginID"] = "annathurai";

Application["DomainName"] = "www.annathurai.com";

}

Example 2: Inside the .aspx page

protected void Page_Load(object sender, EventArgs e)

{

// Code that runs on page load

Application["LoginID"] = "annathurai";

Application["DomainName"] = "www.annathurai.com";

}

How to retrieve application state?

It's very easy to retrieve application state in the ASP.NET application. I am going to take you to write simple application state program now.

string loginID=string.Empty;

loginID = Application["LoginID"].ToString();

string loginID = string.Empty;

loginID = Application.GetKey(0);

We can retrieve all application variable on the currently running application's keys by using HttpApplicationState class.

HttpApplicationState appState = null;

appState = Application.Contents;

String[] StateVars = new String[appState.Count];

StateVars = appState.AllKeys;

How to remove application variable?

We have three methods to remove application variable from the ASP.NET application.

Application.Remove("LoginID");

Application.RemoveAt(0);

Application.RemoveAll();

How to implement Synchronization in application state?

We can avoid deadlock occurrence while we updating application variable by multiple users with help of Lock() and UnLock().

Application.Lock();
Application["LoginID"] = "annathurai";
Application["Domain"] = "www.annathurai.com";
Application.UnLock();

Advantages of application state:

Disadvantages of application state:

Conclusion:

I have give details about application state in asp.net with simple example. I am expecting comments from who are going to read this.