Session State in ASP.NET using C#
In this tutorial, you will learn about session state in ASP.NET.
Definition:
Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead.
Syntax:
Session [“anytime”] = “misnames”;
Benefits of Session State
- Session state is maintained at the session level.
- The session state value is available on all pages within a user session.
- Session’s information is stored on the server.
- Session state persists the data of a particular user in the server.
- This data available till user close the browser or session time completes.
Understanding Session State
To understand the concept of sessions, let’s take an example using code through. So for this follow the steps:
Step 1: Open Visual Studio.
Step 2: Select New Project from the left panel.
Step 3: Select Visual C# and then from .Net Frameworks select ASP.NET Web Application set the name and location and press OK.
Step 4:Select Empty and check web Form and press OK.
Step 5:From solution explorer selects your project name and right-click on it, then select add and then add the Web From.

Step 6:Put a name on the web page.

Step 7: pick two text boxes and one button from the toolbox. Textbox to get a value and then we store it in the session variable and after submitting new page is open where we check that value which we put on the last page this tell us how the session works on all over the site.
ASP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo.aspx.cs" Inherits="WebApplication2.demo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> </div> </form> </body> </html> |
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Session["fname"] = TextBox1.Text; Session["lname"] = TextBox2.Text; Response.Redirect("page2.aspx"); } } } |
Add another page, name it page2.
Similarly, as you created the first page and rename it.
Put a label on page2.
Now the code will be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="page2.aspx.cs" Inherits="WebApplication2.page2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="First Name:"></asp:Label> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> <br /> <asp:Label ID="Label2" runat="server" Text="Last Name: "></asp:Label> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html> |

C# CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class page2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Label3.Text = Session["fname"].ToString(); Label4.Text = Session["lname"].ToString(); } } } |
Put some values in text boxes so that they are same in session variables and we can get them, made on the next page.
Now run the project from the demo.aspx page.
Click on submit button.

This value appears on page2 its just because of session state. Server store session value and when we call it again on another page it will retrieve from the server. Session state is useful for password protected sites very security is an important issue.