Understanding ASP.NET State management techniques-Part V

No.of Views761
Bookmarked0 times
Downloads 
Votes0
By  Vishal Nayan   On  09 May 2011 08:05:50
Tag : ASP.NET , State Management
It store global objects that can be accessed by any client.It supports the same types what session state support which are of type objects, retains information on the server, and uses the same dictionary-based syntax. Application state is based on the System.Web.HttpApplicationState class, which is provided in all web pages through the built-in Application object.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

The Application State is store global objects that can be accessed by any client. It supports the same types what session state support which are of type objects, retains information on the server, and uses the same dictionary-based syntax. Application state is based on the System.Web.HttpApplicationState class, which is provided in all web pages through the built-in Application object.

Points to remember

1)    application state items are stored as objects, so you need to cast them when you retrieve them from the collection
2)    Items in application state never time out
3)    Application state information is always stored in process. This means you can use any .NET data types
4)    Application state isn’t often used, because it’s generally inefficient and its rarely used in practical scenario because its most common uses have been replaced by easier and more efficient methods

1)    We use application state to store database connection string, which is stored now in web.config file, which is more flexible
2)    Previously application state was used to store more frequent data like full product , now you can use ASP.NET cache , which are more efficeint.

Example:

We are often interested into knowing how many times a given page has been requested by various cleint. Using application state variable we can do this;

protected void Page_Load(object sender, EventArgs e)
    {
        int count = 0;
        if (Application["HitCounterForOrderPage"] != null)
        count = (int)Application["HitCounterForOrderPage"];
        count++;
        Application["HitCounterForOrderPage"] = count;    
        lblcounter.Text = "Page Visited: " + count.ToString() + "Times";        
    }

Problem with this approach: In above code, page counter would probably not keep an accurate count, particularly in times of heavy traffic. For example, if two clients requested the page at the same time, you could have a sequence of events like this,

1. User A retrieves the current count (432).
2. User B retrieves the current count (432).
3. User A sets the current count to 433.
4. User B sets the current count to 433.

To avoid this problem you can use mechanism to lock and unlock application state;

Application.Lock();
        int count = 0;
        if (Application["HitCounterForOrderPage"] != null)
        count = (int)Application["HitCounterForOrderPage"];
        count++;
        Application["HitCounterForOrderPage"] = count;
Application.UnLock();
lblcounter.Text = "Page Visited: " + count.ToString() + "Times"; 

When to use: You are storing infrequently changed, application-scope information that is used by many users, and security is not an issue. Do not store large quantities of information in an application state object.

Thank you for reading Hope you enjoyed it.

Download Sample Code

Download source files -12 kb

 
Sign Up to vote for this article
 
About Author
 
Vishal Nayan
Occupation-Software Engineer
Company-
Member Type-Junior
Location-India
Joined date-02 Apr 2011
Home Page-
Blog Page-http://vishalnayan.wordpress.com
Vishal is a seasoned professional with hand on experience on Microsoft technologies. He always look for challenging assignment that allows him to learn newer technologies while utilizing his experience of project development and software engineering ethics. In spare time vishal can be found reading business and political personalities, acts as critics at columnist , writing poetries, bird watching , singing ,cooking. He have strong interests in Indian business and political arena and want to be an active one someday. He is also a part-time trainer on framework , WCF and Silverlight. reach him at vishalnayan@gmail.com
 
 
Other popularSectionarticles
    Cookie is one of several ways to store data about web site visitors during the time when web server and browser are not connected. They are small files That are created on the client’s hard drive (or, if they’re temporary, in the web browser’s memory).
    Published Date : 02/Jun/2011
    It is a storage mechanism that is accessible from all pages requested by a single Web browser session. Therefore, you can use session state to store user-specific information. A session is defined as the period of time that a unique user interacts with a Web application. Active Server Pages (ASP) developers who wish to retain data for unique user sessions can use an intrinsic feature known as session state.
    Published Date : 02/Jun/2011
    Http protocol is stateless, so we need to maintain state on Client side or Server side. For this time I am going to discuss about the maintaining state on Server Side with one programming practice.
    Published Date : 10/Jan/2011
    Now web application framework, no matter how advanced, can change that HTTP is a stateless protocol. So inherently we should also forget our users, but unfortunately we cannot.ASP.Net Framework provides us features by which we can maintain states of your users.
    Published Date : 26/Jun/2010
    There is Lots of ways and Techniques Are available to Manage you State on Web in Aspnet
    Published Date : 06/May/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top