---
title: "Sessions in ASP.Net"  
description: "ASP.NET Session state provides a place to store values that will persist across page requests. Values stored in Session are stored on the server and w"  
author: "Dev Vrat Sahu"  
published: 2012-08-28  
updated: 2019-12-27  
canonical: https://www.mindstick.com/articles/985/sessions-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Sessions in ASP.Net

[ASP.NET](https://www.mindstick.com/articles/155/concept-of-asp-dot-net) [Session state](https://www.mindstick.com/articles/42/session-state-in-asp-dot-net) provides a place to store values that will persist across page requests. Values stored in Session are stored on the server and will remain in [memory](https://yourviews.mindstick.com/story/1186/ayurvedic-tips-for-the-mind-and-memory) until they are explicitly removed or until the [Session expires](https://www.mindstick.com/articles/12042/session-in-c-sharp). \

##### Creating Session

Session["Name"] = txtName.Text;Read Value from Session

StudentName.Text = Session["Name"].ToString();

##### Example

Here is the example which shows you to Create the [**Session and Store**](https://www.mindstick.com/articles/1423/sqlserver-session-state-in-asp-dot-net) the data into it and also [retrieve data](https://www.mindstick.com/forum/159649/how-to-retrieve-data-from-object-tables-on-oracle) from Session.

1. Create the Default.aspx file and the design the following UI as below.

![Sessions in ASP.Net](https://www.mindstick.com/mindstickarticle/7968b471-2129-47c5-97ef-dfd767c378d6/images/4283d649-35be-4118-a418-824d1865f411.png)

Add the following code on the Click [Event Handler](https://www.mindstick.com/forum/1371/c-sharp-dot-net-event-handler-delegate-question) of [Button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) (“Save info in Session”).

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {     }    protected void Button1_Click(object sender, EventArgs e)    {        // This will store the values in Session        Session["ID"] = txtID.Text;        Session["Name"] = txtName.Text;        Session["Course"] =cbCourse.Text;              Response.Redirect("RetriveInforamtiom.aspx");    }} 
```

2. Now, add a new page and name it “RetriveInforamtiom.aspx” and design the following UI.

![Sessions in ASP.Net](https://www.mindstick.com/mindstickarticle/7968b471-2129-47c5-97ef-dfd767c378d6/images/951973af-abff-4433-92c0-90f9a45c0091.png)

Now, on the Page_Load () event of “RetriveInforamtiom.aspx”, write the following codes

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class RetriveInforamtiom : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        //Retriving stored data from Session        StudentID.Text = Session["ID"].ToString();        StudentName.Text = Session["Name"].ToString();        StudentCourse.Text = Session["Course"].ToString();         }} 
```

3. Now, focus on the Default.aspx page and Run the Website. Provide the data for the [required](https://yourviews.mindstick.com/view/81031/vigilence-required-during-corona-pandemic) field and click on “[**Save info in Session**](https://www.mindstick.com/articles/1434/session-in-asp-dot-net)” Button. It will store the data in session and further, it will be redirected to the “RetriveInforamtiom.aspx” page to retrieve and show data from Session.

Here is the output of the above program.

![Sessions in ASP.Net](https://www.mindstick.com/mindstickarticle/7968b471-2129-47c5-97ef-dfd767c378d6/images/7c72ea34-4dbb-4c29-b798-cf966e834b07.png)

---

Original Source: https://www.mindstick.com/articles/985/sessions-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
