Search This Blog

22 June 2011

Session State

what is session:

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session.
Session state information is available to all pages opened by a user during single visit. Therefore, you can use session state to store user specific information. If different user are using your application,each user session has a different session state.


how to create session:


Session["name"]=txtname.Text;
//storing the value of textbox in session
Session["name"]="David";
//storing static value in session
or
Session.Add("name",txtname.Text)
Session.Add("name","David")

how to retrieve session value:

string str=Session["name"].ToString();
lbname.Text=Session["name"].ToString();


how
to destroy session's value :

An important consideration for using Session state is that the Session does expire. By default, if a user does not access their Session data within 20 minutes (by default), the Session will expire and all items that had been stored in the Session will be discarded. Because of this, it is important to check the object that is returned from the Session to see if it exists or if it is null before you try to work with it.

if(Session["name"]!=null)
{
lbname.Text=Session["name"].ToString();
}


The Session Timeout is adjustable through a web.config setting but increasing the timeout value can put memory pressure on your server that may be undesirable.



forcefully destroy the session value, other session methods are:
  • Session.Abandon() - removes the Session and all items that it contains
  • Session.Clear() - removes all items from the Session
  • Session.RemoveAll() - removes all items from the Session
  • Session.Remove("itemName") - removes the item that was stored under the name "itemName"



1 comment:

  1. i want to know about connected environment.... in which we use internal database and no need of sql server 2005...

    ReplyDelete