---
title: "Session in ASP.Net"  
description: "In this article, I’m trying to explain the concept of session in asp.net"  
author: "Sumit Kesarwani"  
published: 2013-05-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1298/session-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# Session in ASP.Net

In this article, I’m trying to [explain the concept](https://www.mindstick.com/forum/158822/explain-the-concept-of-abstract-classes-and-their-significance-in-oop) of session in asp.net\

As we all know, HTTP is a stateless protocol, it can't hold client information on a page. If the user inserts some information and move to the next page, that data will be lost and the user would not be able to retrieve that information. Session provides a facility to store information on server memory. It can support any type of object to store along with our own [custom objects](https://www.mindstick.com/forum/23332/sort-arraylist-of-custom-objects-by-property). For every client, [session data](https://www.mindstick.com/forum/158231/how-can-session-data-be-encrypted-and-what-are-the-benefits-and-drawbacks-of-doing-so) is stored separately, which means session data is stored on a per client basis.

ASP.NET [session state](https://www.mindstick.com/articles/42/session-state-in-asp-dot-net) enables you to [store and retrieve](https://www.mindstick.com/forum/158470/how-can-you-store-and-retrieve-data-in-the-client-side-cache-using-javascript) values for a user as the user navigates ASP.NET pages in a [Web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about). 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](https://www.mindstick.com/forum/157371/swap-two-integers-variable-values-without-using-the-third-variable) for the duration of that session.

##### Advantages of Session:-

1. It helps maintain user state and data all over the application.
2. It is easy to implement and we can store any kind of object.
3. Stores [client data](https://www.mindstick.com/news/2355/data-protection-legislation-will-stop-the-exploitation-of-client-data-and-punish-offenders) separately.
4. Session is [secure and transparent](https://answers.mindstick.com/qa/99894/explain-the-role-of-blockchain-technology-in-ensuring-secure-and-transparent-transactions) from the user.

##### Example:-

##### Step 1:-

Design the form as shown below:-

![Session in ASP.Net](https://www.mindstick.com/mindstickarticle/e06ebdb3-26ad-4592-ba9a-3e32574b6596/images/48cee9fe-e50f-4e13-ab6e-6351a7295805.png)

##### Step 2:-

Write the code in the webform1.aspx.cs

```
using System;

namespace SessionWebApplication{    public partial class WebForm1 : System.Web.UI.Page    {        protectedvoid Page_Load(objectsender, EventArgse)        {

        }
        protectedvoid btnSave_Click(objectsender, EventArgse)        {            //storing data in Session variable name and city            Session["Name"] =txtName.Text;            Session["City"] =txtCity.Text;            txtName.Text="";            txtCity.Text="";            lblMsg.Text="Data saved in session";
        }

    }

}
```

Step 3:-

##### Webform2.aspx

#####

```
    <div>        <asp:Label ID="lblName"runat="server"></asp:Label>        <br/>        <asp:Label ID="lblCity"runat="server"></asp:Label>        <br/>        <br/>        <asp:HyperLink ID="HyperLink1" runat="server"NavigateUrl="~/WebForm1.aspx">Back</asp:HyperLink>
    </div> 
```

#####

#####

#####

#####

#####

#####

#####

Step 4:-

Write code in webform2.aspx.cs

```
using System; 
namespace SessionWebApplication{
    public partial class WebForm2 : System.Web.UI.Page    {        protectedvoid Page_Load(objectsender, EventArgse)        {            //retrieving data from session variable name and city            lblName.Text="Hi "+Session["Name"].ToString();            lblCity.Text="You live in "+Session["City"].ToString();
        }
    }
} 
```

##### Step 5:-

Run the application

![Session in ASP.Net](https://www.mindstick.com/mindstickarticle/e06ebdb3-26ad-4592-ba9a-3e32574b6596/images/66edc452-67d5-42c0-80c8-83cec77b2e8d.png)

After filling values in the textboxes click on the save to session button

![Session in ASP.Net](https://www.mindstick.com/mindstickarticle/e06ebdb3-26ad-4592-ba9a-3e32574b6596/images/98cc8d2c-33cd-4c5e-9e6d-af1698f73a0a.png)

You will get a message that data saved in session and then click on Test Session

![Session in ASP.Net](https://www.mindstick.com/mindstickarticle/e06ebdb3-26ad-4592-ba9a-3e32574b6596/images/7c29340a-842d-4b6d-ae81-79de38a21c60.png)

You will be redirect to webform2.aspx and get the message like this.

---

Original Source: https://www.mindstick.com/articles/1298/session-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
