State Management in ASP.NET

No.of Views594
Bookmarked0 times
Downloads 
Votes1
By  usamawahabkhan   On  26 Jun 2010 03:06:44
Tag : ASP.NET , State Management
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.
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

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.

We can Manage State on Application ans Session Level

  • Session State
  • Application State

Session State

It stores user specific information on server side. There are 5 modes which a user can define for Session States.

  1. Off
  2. InProc
  3. StateServer
  4. SQLServer
  5. Custom

Sessions can and cannot be cookie based. The default name for this Session based cookie is Asp.Ner_SessionId. Session state is structured as a key/value dictionary for storing session-specific information that needs to be maintained between server round trips and between requests for pages.

You can use session state to accomplish the following tasks:

  • Uniquely identify browser or client-device requests and map them to an individual session instance on the server. 
  • Store session-specific data on the server for use across multiple browser or client-device requests within the same session. 
  • Raise appropriate session management events. In addition, you can write application code leveraging these events

Sessions (In Proc)

When the configuration is set to InProc, session data is stored in the HTTPRuntime’s internal cache in an implementation of ISessionItemCollection that implements ICollection. The Session in Stores ASP.Net in memory cache.

Sessions (State Server)

Also known as out of process.

  • Sessions states are held in a process called aspnet_state.exe that runs as a Windows Service.
  • Objects to be stored in StateServer sessions require being Serializable.
  • By default State Service is not running.

Sessions (Sql Server)

Sql Server based state management is not effected by farming or gardening issues.Enable Sql Server support by running aspnet regsql -? Command to access and enable Session State Option.

Aspnet_regsql –S machine\servername –d statemanagement –U sa –P new –ssadd –sstype c

Sessions Recommendations

  • InProc is the fastest but most unreliable
  • StateServer is a better option but requires Serialization. It is slower than InProc as objects physically leave the application.
  • SQLServer is the most reliable and robust but is the slowest but also helpful when it comes to application scalability.

Declaring a Session Variable

string firstName = "Usama";
string lastName = "Khan";
string city = "Karachi";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;

Using Session Variable

TxtfirstName.Text = Session["FirstName"];
TxtlastName.Text = Session["LastName"] ;
Txtcity.Text = Session["City"];

 Application State

Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

Global does not mean global to the machine, but global to the application.

global.asax

Application state is stored in an instance of the HttpApplicationState class. This class exposes a key-value dictionary of objects.

Declaring Application Variable

In Application_Start handler of your application's Global.asax

Application["Message"] = "Welcome to Evolution Technologies ";
Application["PageRequestCount"] = 0;
Application.Lock();
Application["PageRequestCount"] =(int)Application["PageRequestCount"])+1;
Application.UnLock();

 That's all, i hope this is help to you all.

 
Sign Up to vote for this article
 
About Author
 
usamawahabkhan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Pakistan
Joined date-06 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
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
    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.
    Published Date : 09/May/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
    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