Overview Of Session In ASP.NET

Introduction

 
Session is a state management technique. A session can store the value on the server. The session object is used to store and retrieve specific values within a web page and it stores and retrieves the value of the particular time session. Session is one of the best techniques of State Management because it stores some information or data and is used to pass it from one page to another. I have written this article focusing on students and beginners.
 

Flow Chart

 
Overview Of Session In ASP.NET
Fig: 1
 

Session Timeout

  1. Session's default timeout is 20 minutes.
  2. We can set the session timeout manually in web.config or programmatically.
Session Timeout in programmatically
  1. Session.Timeout = 10;  
Session Timeout in web.config
  1. <configuration>  
  2.   <system.web>  
  3.       <sessionState timeout="30"></sessionState>  
  4.       </system.web>  
  5. </configuration>  

How to create a Session

 
Step 1
  1. Right-click on Solution Explorer then “Add”, select “Web Form”.
  2. Create a webform page named as login (or choose the name you wish).
  3. Create an event and write the following code at Login.aspx.cs.
Coding
  1. protected void btnLogin_Click(object sender, EventArgs e)  
  2.   {  
  3.      Session["UserName"] = txtEmail.Text;  
  4.      Session["PassWord"] = txtPassword.Text;  
  5.      Response.Redirect("User.aspx");  
  6.   }  
Output
 
Overview Of Session In ASP.NET
 
Step 2
  1. Create another webform page named as User (or choose the name you wish).
  2. Write the following code at User.aspx.cs.
Coding
  1. protected void Page_Load(object sender, EventArgs e)  
  2.   {  
  3.      if (!IsPostBack)  
  4.        {  
  5.           if (Session["UserName"] != null)  
  6.               {  
  7.                 lblUser.Text = "Welcome to" + "  " + Session["UserName"].ToString();  
  8.               }                  
  9.        }  
  10.   }  
Output
 
Overview Of Session In ASP.NET
 

Session Mode

 
Overview Of Session In ASP.NET
Fig: 2
 
InProc
  1. This is default session state mode.
  2. Sessions are stored inside of the application's process on the webserver.
    1. <configuration>  
    2.    <system.web>  
    3.       <sessionState mode="InProc"></sessionState>  
    4.    </system.web>  
    5. </configuration>  
OutProc
 
Session data are stored in different memory locations.
  1. SQL Server.
  2. State Server.

SQL Server

  1. Session data are stored in different Servers.
  2. SQL Server database is used to store session data and SQL Server connects with ConnectionString.
  3. Write the following code at web.config.
    1. <configuration>  
    2.    <system.web>  
    3.       <sessionState mode="SQLServer" sqlConnectionString="ConnectionString"></sessionState>  
    4.    </system.web>  
    5. </configuration> 

State Server

  1. Session data are stored in different servers.
  2. Sessions are stored using State Server windows service.
  3. Write the following code at web.config
    1. <configuration>  
    2.    <system.web>  
    3.       <sessionState mode="StateServer" stateConnectionString="ConnectionString"></sessionState>  
    4.    </system.web>  
    5. </configuration> 
Advantages
  1. It is easy to implement and accessing data is very fast.
  2. Session is secure and transparent from the user.
  3. Sessions are very simple to use.

Summary

 
In this article, I discussed sessions in ASP.NET. I have written this article focusing on beginners and students.


Similar Articles