Application State in ASP.Net

ASP.Net Application State

ASP.Net Application state is a server side state management technique.

Application state is a global storage mechanism that used to stored data on the server and shared for all users, means data stored in Application state is common for all user. Data from Application state can be accessible anywhere in the application. Application state is based on the System.Web.HttpApplicationState class.

The application state used same way as session state, but session state is specific for a single user session, where as application state common for all users of application.

Syntax of Application State

Store information in application state

  • Application[“name”] = “Meera Academy”;

Retrieve information from application state

  • string str = Application[“name”].ToString();

Example of Application State in ASP.Net

Generally we use application state for calculate how many times a given page has been visited by various clients.

Design web page in visual studio as shows in below figure.

Application State management in ASP.Net
Application State management in ASP.Net

Here, we calculate total visit of uses visited web page by clicking “Click to Visit” button.

C# Code for Example

Output of Example

Application State Example in ASP.Net
Application State Example in ASP.Net

Here, above output screen we use different browser for visit same page. The result counter value stored in Application object so it would be changed simultaneously for both visitors.

In above case some time too many users click button at the same time, that time result wont be accurate. This situation known as dead lock. To avoid dead lock we use Lock() and UnLock() in application state.

Lock() and UnLock() in Application State

Session State Example in ASP.Net

Now, do above same example using Session state.

Session State Example in ASP.Net
Session State Example in ASP.Net

C# code for above example


Leave a Reply

Your email address will not be published. Required fields are marked *