In this article, I will demonstrate you about session in asp.net. I will explain you why we use it and how to implement in code.

What is Session?

Session is a state management technique which is used to manage the state of page or control throughout the application. So, I mean to say that using the session we can store the value and access it another page or throughout the application.

Why Session?

As you know that the HTTP is the stateless protocol, it means every request is taking as independent request. Browser does not know about previous request and data. So, every previous request data goes lost and you cannot retrieve previous data. Basically using the session, we store the data in session and access it anywhere as per requirement. It provides application level state management.

There is lots of other technique which is used for state management like ViewState, Cookie etc. There is default time to expire the session, you can increase and decrease it using the configuration.

 

<sessionState mode="SQLServer"  cookieless="true"  regenerateExpiredSessionId="true" timeout="30" sqlConnectionString="Data Source=MySqlServer;Integrated Security=SSPI;"  stateNetworkTimeout="30"/>

 

 
Advantages of Session

First and most important it is used to maintain the state of user data throughout the application. It stores any type of object. Using the session, you can add variable values as well as any type of object like object of class, list, datatable etc. It is secure.

 
Disadvantages of Session

It makes your website slow, if you use it. It is because the session value stores on server memory. So, every time it need to serialize the data before the storing it and desterilize the data before to show the user.

 

Example

Following is the example which shows you how can you store the session value in one page and access it on another page.

First Page

// Set Session values during button click
protected void btnSubmit_Click(object sender, EventArgs e)
{
     Session["FirstName"] = txtfName.Text;
     Response.Redirect("Default2.aspx");	
}

Second Page

// Get Session values 

if (Session["FirstName"]!= null)
{
   lblString.Text = Session["FirstName"];
}

 

Following is the example with code.

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Asp.net Session</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h3>Default.aspx</h3>

<table>

<tr>

<td>FirstName:</td><td><asp:TextBox ID="txtfName" runat="server"/></td>

</tr>

<tr>

<td>LastName:</td><td><asp:TextBox ID="txtlName" runat="server"/></td>

</tr>

<tr><td></td><td> <asp:Button ID="btnSubmit" runat="server" Text="Set SessionState Data" OnClick="btnSubmit_Click" /></td></tr>

</table>

</div>

</form>

</body>

</html>

 

Codebehind C# Code for Default.aspx.cs

// Set Session values during button click

protected void btnSubmit_Click(object sender, EventArgs e)

{

Session["FirstName"] = txtfName.Text;

Session["LastName"] = txtlName.Text;

Response.Redirect("Default2.aspx");

}

 

Default2.aspx

Second page, where you need to access the session values.

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h3>Default2.aspx</h3>

<table>

<tr>

<td colspan="2">Welcome <b><asp:Label ID="lblString" runat="server"/></b></td>

</tr>

<tr>

<td>Your FirstName: </td><td><b><asp:Label ID="lblfName" runat="server"/></b></td>

</tr>

<tr>

<td>Your LastName </td><td><b><asp:Label ID="lbllName" runat="server"/></b></td>

</tr>

<tr><td></td><td> </td></tr>

</table>

</div>

</form>

</body>

</html>

 

Codebehind C# Code for Default2.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

if (Session["FirstName"] == null && Session["LastName"]==null)

{

Session["FirstName"] = "Aspdotnet";

Session["LastName"] = "  Suresh";

lblString.Text = "Welcome " + Session["FirstName"] + Session["LastName"];

}

else

{

lblString.Text = Session["FirstName"]+" " + Session["LastName"];

lblfName.Text = Session["FirstName"].ToString();

lbllName.Text = Session["LastName"].ToString();

}

}

}
 
Conclusion:

So, finally in this article you see that what is session in asp.net and why we use with sample code example.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks