Home > DeveloperSection > Articles > Session in ASP.NET

Session in ASP.NET


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 2406  View(s)
Rate this:

Session in ASP.NET

 

In this article, I’m explaining the concept of session in asp.net.

 

As we all know that, HTTP by nature is stateless protocol. It cannot hold client information on a page. If the user inserts some information and move to the next page, that data will be lost and the user would not be able to retrieve that information. Session allows information to be stored in one page and access in another page and support any type of object. For every client, session data is stored separately, that means session data is stored on a per client basis.

There are following advantages of using session:

·         Accessing of data is very fast as it stores session data in memory object of the current application domain.

·         It helps maintain user state and data all over the application.

·         It stores client’s data separately.

·         It is easy to implement and we can store any kind of object.

·         Session is secure and transparent from the user.

There are following disadvantages of session:

·          As the session state data is stored in server memory, it is not advisable to use session when you are working with large sum of data.

Some Important methods of session:

·         Session.Abandon() - Removes the Session and all items that it contains

·         Session.RemoveAll() - Removes all items from the Session

·         Session.Remove ("SessionName") - Removes the item that stored under the name "SelectedName".

·         Session.Clear() - Removes  items from the Session

Example

Step 1

Crete a form as shown below:

Session in ASP.NET

Step 2:

Write the code on webForm1.aspx.cs.

using System;

 

public partial class webForm1 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnSaveSession_Click(object sender, EventArgs e)

    {

        //storing data in Session variable name and city

        Session["Name"] = txtName.Text;

        Session["Age"] = txtAge.Text;

        Session["Address"] = txtAddress.Text;

      

        // clear textbox after store in session

        txtName.Text="";

        txtAge.Text="";

        txtAddress.Text="";

        // give a message to save

        lblMsg.Text = "Data has been saved in session";

    }

    protected void btnGoNextPage_Click(object sender, EventArgs e)

    {

        Response.Redirect("webForm2.aspx");

    }

}

Step 3

Write webform2.aspx code:

<div>

      Hi ! <asp:Label ID="lblName" runat="server" Text=""></asp:Label>

      <br />

      Your Age is: <asp:Label ID="lblAge" runat="server" Text=""></asp:Label> and address is: <asp:LabelID="lblAddress" runat="server" Text=""></asp:Label>.

      <br />

      <br />

        <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />

    </div>

Step 4

Write the code on webForm2.aspx.cs.

using System;

 

public partial class webForm2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

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

        lblAge.Text = Session["Age"].ToString();

        lblAddress.Text = Session["Address"].ToString();

    }

    protected void btnBack_Click(object sender, EventArgs e)

    {

        Response.Redirect("webForm1.aspx");

    }

}

Step 5

Run the application:

Session in ASP.NET

After fill the data click to Store in Session button Session in ASP.NET

If you want to show saved data then click Go Next Page button. Back button go to previous page.

 

Session in ASP.NET


Don't want to miss updates? Please click the below button!

Follow MindStick