---
title: "Session in ASP.NET"  
description: "In this article, I’m explaining the concept of session in asp.net."  
author: "Anchal Kesharwani"  
published: 2014-06-25  
updated: 2020-02-01  
canonical: https://www.mindstick.com/articles/1434/session-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Session in ASP.NET

In this article, I’m explaining the concept of session in asp.net.

As we all know that, HTTP by nature is stateless protocol. It cannot 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 allows information to be stored in [one page](https://www.mindstick.com/forum/118/problem-in-display-content-of-one-page-to-another-page) and access in another page and support any type of object. 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, that means session data is stored on a per client basis.

There are following [advantages of using session](https://answers.mindstick.com/qa/30663/what-are-the-advantages-of-using-session):

· Accessing of data is very fast as it stores session data in memory object of the current [application domain](https://www.mindstick.com/interview/718/what-is-an-application-domain).

· It helps maintain user state and data all over the application.

\
· It stores client’s data separately.

\
· It is easy to implement and we can store any kind of object.

\
· 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.

\
There are following [disadvantages](https://yourviews.mindstick.com/view/83828/advantage-or-disadvantages-of-health-insurance) of session:

\
· As the [session state](https://www.mindstick.com/articles/42/session-state-in-asp-dot-net) data is stored in server memory, it is not advisable to use session when you are working with large sum of data.

Some Important methods of session:

· [Session.Abandon](https://www.mindstick.com/interview/23267/difference-between-session-abandon-and-clear)() - Removes the Session and all items that it contains

· Session.RemoveAll() - Removes all items from the Session

· Session.Remove ("SessionName") - Removes the item that stored under the name "SelectedName".

· Session.Clear() - Removes items from the Session

##### Example

##### Step 1

\

Crete a form as shown below:

![Session in ASP.NET](http://www.mindstick.com/MindStickArticle/d1561d32-820f-4e1c-bd32-21567d2155e7/Images/image001.png)

##### Step 2:

\

Write the code on webForm1.aspx.cs.

```
using System; public partial class webForm1 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {     }    protected void btnSaveSession_Click(object sender, EventArgs e)    {        //storing data in Session variable name and city        Session["Name"] = txtName.Text;        Session["Age"] = txtAge.Text;        Session["Address"] = txtAddress.Text;              // clear textbox after store in session        txtName.Text="";        txtAge.Text="";        txtAddress.Text="";        // give a message to save        lblMsg.Text = "Data has been saved in session";    }    protected void btnGoNextPage_Click(object sender, EventArgs e)    {        Response.Redirect("webForm2.aspx");    }}
```

##### Step 3

\

Write webform2.aspx code:

```
<div>      Hi ! <asp:Label ID="lblName" runat="server" Text=""></asp:Label>      <br />      Your Age is: <asp:Label ID="lblAge" runat="server" Text=""></asp:Label> and address is: <asp:LabelID="lblAddress" runat="server" Text=""></asp:Label>.      <br />      <br />        <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />    </div>
```

##### Step 4

\

Write the code on webForm2.aspx.cs.

```
using System; public partial class webForm2 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        lblName.Text = Session["Name"].ToString();        lblAge.Text = Session["Age"].ToString();        lblAddress.Text = Session["Address"].ToString();    }    protected void btnBack_Click(object sender, EventArgs e)    {        Response.Redirect("webForm1.aspx");    }}
```

##### Step 5

\

Run the application:

![Session in ASP.NET](http://www.mindstick.com/MindStickArticle/d1561d32-820f-4e1c-bd32-21567d2155e7/Images/image002.png)

After fill the data click to Store in Session button

![Session in ASP.NET](http://www.mindstick.com/MindStickArticle/d1561d32-820f-4e1c-bd32-21567d2155e7/Images/image003.png)

If you want to show saved data then click Go Next Page button. Back button go to

previous page.

![Session in ASP.NET](http://www.mindstick.com/MindStickArticle/d1561d32-820f-4e1c-bd32-21567d2155e7/Images/image004.png)

---

Original Source: https://www.mindstick.com/articles/1434/session-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
