How to Create, Retrieve and Remove Application State in Asp.Net

Leave a Comment

How to Create Application State in Asp.Net

It is 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 Appliation_Start(object sender, EventArgs e)
{
  //Code that runs on application startup
  Application["LoginID"] = "annathural";
  Application["DomainName"] = "www.annathural.com";
}

Example 2 : Inside the .aspx page
protected void Page_Load(object sender, EventArgs e)
{
  //Code that runs on Page Load
  Application["LoginID"] = "annathural";
  Application["DomainName"] = "www.annathural.com";
}

How to retrieve application State?

It  is 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();

0 comments:

Post a Comment