Menu ▼

Previous Next

State management concepts: Session will be used when we need to maintain user level data when user navigating to multiple pages with in the application.

Assign value to a session

Session["UserName"] = txtUserName.Text.ToLower();

Reading value from session

if (Session["UserName"] != null)


    lbUserName.Text = Session["UserID"].ToString();



    There are two types of session modes
  • Session In-Process (InProc)
  • Session Out-Process (OutProc)

InProc :
By Default Session state will be saved in-process, i.e. in the system/server memory where the application is running. Mostly we will use inproc sessions because it will give better performance.

OutProc :
Out proc session is two types

  • State Server
  • SQL Server
State Server
If we are using State Server then the session will be maintained by the ASP.NET State server service. so before using this state we should make sure that ASP.NET State server service is running on the server (can be the same server where we hosted the web applciation or can be a different server). If we are using State server mode then the session will be availble even though the web applicaiton is restarted. And can access the same session from multiple web servers. This state will give us less performance compare to Inproc session state.

SQL Server
If we are using the SQL Server as session mode then the session will be stored in SQL Server. So the Session will be still available if the web application is restarted and for the multiple we servers. this will give less performance compare to Inproc session mode and State server session mode.

We can configure the state mode in the web config file. the below example will give you an idea how to do that.




          sqlConnectionString="Data Source=localhost\SQLEXPRESS;user id=sa;password=pwd;data source=dbname;" />

mode : Session mode - Inproc, SQLServer, StateServer
stateConnectionString : if session mode is StateServer then we have to use this tag and set the server connection string.
sqlConnectionString : if session mode is SQLServer then we have to use this tag and set the SQL connection string.
Previous Next