ASP.NET Creating and Retrieving Sessions example

ASP.NET Creating and Retrieving Sessions example 022

ASP.NET Creating and retrieving Sessions example

Sessions in ASP.NET are used to store and retrieve values and information for a user. ASP.NET stores sessions data for each user on server-side memory, hence session provide a powerful option of storing information of a transactions made by different user on a web page. ASP.NET Creating and retrieving Sessions example.

Sessions expire after a certain amount of time. You can define this duration through the code file or web.config file in your .NET project.
How to store and retrieve values from session? :
Let’s take a example of a login page in ASP.NET page. On Login page code file, a session with UserId which can be a primary key can be stored in sessions. ASP.NET session example with MS SQL server database.

Code for storing session :

Session[“userid”] = userid;// value retrieve from database after successful authorization

And we can retrieve this userid on our Index or Homepage to make loading secure only to specific users :

if(Session[“userid”]==null)
Response.Redirect(“Login.aspx”);
else
lblWelcome.Text = “Welcome user”;

Now we will create a working example to see the working of sessions in ASP.NET.

Create a new ASP.NET project. You can also use existing project. Create two .aspx pages with name :

  1. FrmDetails.aspx
  2. FrmViewDetails.aspx

Open the first page FrmDetails and edit is as below code.

FrmDetails.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDetails.aspx.cs" Inherits="FrmDetails" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Enter your details</title>
</head>
<body>
 <form id="form1" runat="server" style="padding: 5px; font-family: Arial; color: #1c1c1c;">
 <div>
 <h2>
 Enter your details</h2>
 <hr />
 Name : &nbsp;&nbsp;
 <asp:TextBox runat="server" ID="txtname" />
 <br />
 <br />
 Age : &nbsp;&nbsp;&nbsp;
 <asp:TextBox runat="server" ID="txtAge"/>
 <br />
 <br />
 Select a color : &nbsp;&nbsp;
 <asp:DropDownList runat="server" ID="ddColor">
 <asp:ListItem Text="Red" Value="Red"></asp:ListItem>
 <asp:ListItem Text="Green" Value="Green"></asp:ListItem>
 <asp:ListItem Text="Black" Value="Black"></asp:ListItem>
 <asp:ListItem Text="Purple" Value="Purple"></asp:ListItem>
 <asp:ListItem Text="Yellow" Value="Yellow"></asp:ListItem>
 <asp:ListItem Text="Blue" Value="Blue"></asp:ListItem>
 <asp:ListItem Text="Violet" Value="Violet"></asp:ListItem>
 <asp:ListItem Text="Orange" Value="Orange"></asp:ListItem>
 </asp:DropDownList>
 <asp:Button runat="server" Text="Proceed" ID="btnProceed" 
 onclick="btnProceed_Click" />&nbsp;&nbsp;
 <asp:Button runat="server" Text="Clear" ID="btnClear" onclick="btnClear_Click" 
 style="height: 26px" />
 <br />
 <br />
 <asp:Label runat="server" ID="lblMessage" />
 </div>
 </form>
</body>
</html>

This page contains two text boxes for entering Name and age of the user and one drop down list to select a color from the list. We will add these three values of our ASP.NET sessions and retrieve these values from ASP.NET sessions on next page on its page load method and showing it on a label.

ASP.NET Creating and Retrieving Sessions example 01

ASP.NET Creating and Retrieving Sessions example

FrmDetails.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class FrmDetails : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {

 }
 protected void btnProceed_Click(object sender, EventArgs e)
 {
 Session["name"] = txtname.Text;
 //adding string values to asp.net session

 Session["age"] = Convert.ToInt32(txtAge.Text);
 // adding integer values to asp.net session

 Session["colorname"] = ddColor.Text.ToString();
 // adding values from dropdown list as String in asp.net session
 Response.Redirect("FrmViewDetails.aspx");
 }
 protected void btnClear_Click(object sender, EventArgs e)
 {
 txtname.Text = "";
 txtAge.Text = "";
 

 }
}

ASP.NET Creating and Retrieving Sessions example 02

ASP.NET Creating and Retrieving Sessions example

Edit the FrmViewDetails.aspx page as below :

FrmViewDetails.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmViewDetails.aspx.cs" Inherits="FrmViewDetails" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>View Details</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Label runat="server" ID="lblDetails"></asp:Label>
 </div>
 </form>
</body>
</html>

And its code file as below :

FrmViewDetails.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

public partial class FrmViewDetails : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 String username = Session["name"].ToString();
 // retrieving asp.net session value from string

 String age = Convert.ToString(Session["age"]);
 // retrieving asp.net session value from int

 String colorname = Session["colorname"].ToString();
 // retrieving asp.net session value from string

 lblDetails.Text = "<b>Enter name : " + username + " and Age : " + age + " years.</b>";
 lblDetails.ForeColor = Color.FromName(colorname);
 }
}

Now set FrmDetails.aspx page as your start page and run the project.


2 thoughts on “ASP.NET Creating and Retrieving Sessions example”

  1. Pingback: ASP.NET Creating and retrieving Sessions with MS SQL server • ParallelCodes;

  2. Pingback: ASP.NET Login page using Sessions • ParallelCodes;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.