Session State Management in ASP.Net 2.0
Session state management consists of storing values (parameters, objects etc) related to a session of one user. The values used in a session can be used from any page of the website until the session ends.
There are two important methods for storing session – In Proc and out proc
In Proc method stores the value in ASP.NET worker process in web server.
This is the fastest method. But, this method has some flip sides – this process can restart any time – It can restart if you change some value in Web.Config or Global.asax. Then, the whole session data is lost.
Also, in the case of a web garden, if you have more than one web servers, each server will have separate session details leaving some disastrous results.
You can set the session state mode in System.Web part of web.config file.
<sesseionstate mode= “InProc” Cookieless= “false” timeout= “20”/> note that the timeout is specified in seconds…
You can store objects in session as key value pair – using a string key as index.
Ex:
Session[“BOSENAME”]= “Donald Duck”;
Note that everything is stored as objects in Session
Now, you can retrieve it from any page in the application, giving
String a;
a=(string)Session[“BoseName”]
Note we are explicitly converting the session object to string…
To get a strongly typed value, you can create a page with soe properties attached.
C#-
Public class Mypage: System.Web.UI.Page // Here we are inheriting Page class so that the new class will be having all the properties of page.
Private const string BossName;
Public string BossName()
{
Get
{
BossName=(string)Session[“BOSSNAME”];
Return value;
}
Set
{
Session[“BOSSNAME”]= BossName;
}
}
}
Now you have to inherit this class for your pages instead of System.Web.UI.Page
Then if you want to add some string to the state variable “BOSSNAME”, you have to just type
String a= Donald Duck;
BossName= a;
You can retrieve it as a=BossName;
Session state management consists of storing values (parameters, objects etc) related to a session of one user. The values used in a session can be used from any page of the website until the session ends.
There are two important methods for storing session – In Proc and out proc
In Proc method stores the value in ASP.NET worker process in web server.
This is the fastest method. But, this method has some flip sides – this process can restart any time – It can restart if you change some value in Web.Config or Global.asax. Then, the whole session data is lost.
Also, in the case of a web garden, if you have more than one web servers, each server will have separate session details leaving some disastrous results.
You can set the session state mode in System.Web part of web.config file.
<sesseionstate mode= “InProc” Cookieless= “false” timeout= “20”/> note that the timeout is specified in seconds…
You can store objects in session as key value pair – using a string key as index.
Ex:
Session[“BOSENAME”]= “Donald Duck”;
Note that everything is stored as objects in Session
Now, you can retrieve it from any page in the application, giving
String a;
a=(string)Session[“BoseName”]
Note we are explicitly converting the session object to string…
To get a strongly typed value, you can create a page with soe properties attached.
C#-
Public class Mypage: System.Web.UI.Page // Here we are inheriting Page class so that the new class will be having all the properties of page.
Private const string BossName;
Public string BossName()
{
Get
{
BossName=(string)Session[“BOSSNAME”];
Return value;
}
Set
{
Session[“BOSSNAME”]= BossName;
}
}
}
Now you have to inherit this class for your pages instead of System.Web.UI.Page
Then if you want to add some string to the state variable “BOSSNAME”, you have to just type
String a= Donald Duck;
BossName= a;
You can retrieve it as a=BossName;