.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Hour 5: Understanding 5 ASP.NET State management techniques in 5 hours

Posted By:Vishal Nayan       Posted Date: April 30, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Application State: 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.
 

Application State: 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.

 

 

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.

 

 

This is end of your 5th hour of reading Hope you enjoyed it.

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend