Application state is used to store ![]()
![]()
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:
-
Application object memory relased when we removed.
-
Multi user can able to access application variable.
-
To avoid deadlock or conflict we should use Lock and Unlock when we use write or update in the application object.
-
Other application can't access this application values.
Disadvantages of application state:
-
Application variable will exists until exit our application.
-
If we do not have Lock() and Unlock, deadlock will occur.
-
Its gloable variable so anyone can access within this application.
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.
Sudhir DehadePosted Apr 20, 2018, 9:32 AM
Mast re Bhava...........................
Mohammad AwaisPosted Dec 7, 2015, 12:23 PM
Informative post good work.....................!!!
Navjot SinghPosted Dec 12, 2012, 3:41 AM
this is good article.
Anand upadhyayPosted May 28, 2012, 6:16 AM
very nice article for me.
Bawal BajPosted Jul 20, 2011, 3:03 AM
this section is very good , i am gonna use this ..
ashok kumarPosted Nov 9, 2010, 5:39 AM
Very good one for begginers friend! Keep posting