Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use Session Object in ASP.NET

Author: Nikki Smith
Download Source Code :

In this article, you will learn how to store value in a session object in a page and retrieve this session value in another page.


By default, Session state values are stored in memory using InProc mode. There are other Modes to store session state values like SQL Server and State Server. For further detail about Session State, refer to my article here on DevASP.NET. The below example will show in process mode to store session state. You can also download source code.


  1. Create a new Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add two Web Forms in the Web Site.
  3. Write <sessionState> tag in between <System.web> tag in web.config file

           <sessionState mode ="InProc" cookieless ="true" timeout ="10"/>

    Session values will be stored in-memory as the mode is set to in process. Timeout to expire session is set to 10 minutes and cookieless is set to true.
  4. Write code below in Default.aspx page.

    <asp:Label ID="Label1" runat="server" Text="First Name:"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label2" runat="server" Text="Last Name:"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label3" runat="server" Text="Country"></asp:Label>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save Session Values" />
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx">See Session Values on Second Page</asp:HyperLink>

  5. The button click event of the code behind file (Default.aspx.vb or Default.aspx.cs) will be like this:

    Visual Basic

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim firstName As String = TextBox1.Text
        Dim secondName As String = TextBox2.Text
        Dim country As String = TextBox3.Text
        Session("FirstName") = firstName
        Session("SecondName") = secondName
        Session("Country") = country
        lblMessage.Text = "Values are stored in session objects"
    End Sub

    Visual C#

    protected void Button1_Click(object sender, EventArgs e) {
        string firstName = TextBox1.Text;
        string secondName = TextBox2.Text;
        string country = TextBox3.Text;
        Session["FirstName"] = firstName;
        Session["SecondName"] = secondName;
        Session["Country"] = country;
        lblMessage.Text = "Values are stored in session objects";
    Text boxes values are assigned to string variables are then these values are stored in Session objects by using different key for every value.
  6.  Write code below in Default2.aspx page

    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Back to First Page</asp:HyperLink>

  7.  Write code below in Page Load event in code behind file (Default2.aspx.vb or Default2.aspx.cs)

    Visual Basic
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Session("FirstName") IsNot Nothing And Session("SecondName") IsNot Nothing And Session("Country") IsNot Nothing Then
                Dim firstName As String = CType(Session("FirstName"), String)
                Dim secondName As String = CType(Session("SecondName"), String)
                Dim country As String = CType(Session("Country"), String)
                Label1.Text = "Your First Name is: " + firstName
                Label1.Text += " , Your Second Name is: " + secondName
                Label1.Text += " And Your Country is: " + country
                Label1.Text = "Session values are not available"
            End If
        End Sub

    Visual C#
    protected void Page_Load(object sender, EventArgs e) {
      if (Session["FirstName"] != null && Session["SecondName"] != null && Session["Country"] != null) {
           string firstName = Session["FirstName"].ToString();
           string secondName = Session["SecondName"].ToString();
           string country = Session["Country"].ToString();
           Label1.Text = "Your First Name is: " + firstName;
           Label1.Text += " , Your Second Name is: " + secondName;
           Label1.Text += " And Your Country is: " + country;
       else {
           Label1.Text = "Session values are not available";

    First you have to check that session is not null and after that retrieve session values.
  8. Press F5 and see the web site in browser. Provide values, click on button. Now click on the link and go the next page. You can see the values retrieved from session objects.


Add Article Comment:
Name :
Email Address :
Comments :
<< Session State in ASP.NET

Disclaimer - Privacy
© 2002-2017