Sunday, December 2, 2012

Session, ViewState and Application State in ASP.NET

Session, ViewStates and Application State are used in ASP.NET for the purpose of storing data temporary.

Declared Session can be retrieved from any where of the application during its life time.
Session is created on user context means that session will be created for each user browsing the application.
Default session time out is 20 mins.
Session time out can be configured through web.config and IIS server settings.
<sessionState timeout="60" />

ViewState exists only between page post backs.
ViewState is lost if the session state is lost.

For storing data it is recommended to use a Session variable because processing time of ViewState gets very slow if huge amount of data is stored in the ViewState and  ViewState can be easily lost.

ApplicationState is common through the whole application and for the all users.
ApplicationState can be used to get the number of logged in users in the application.

DataTable DT=new DataTable();
DT=GetValues();//Method for getting a datatable
Session["a"] = "test ViewState";
ViewState["b"] = "test ViewState";
Application["c"] = "test Application";
Session["Data"]= DT;

//Retrieving Values

string a = Session["a"].ToString();
string b = ViewState["b"].ToString();
string c = Application["c"].ToString();
DT=  (DataTable)Session["Data"];

No comments:
Write comments
Recommended Posts × +