Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

Click here to download the source code for this lesson.

This lesson builds on the previous lesson regarding ASP.NET redirects.  In this lesson I will cover the ASP.NET Session object.  When you are implementing a requirement that needs information shared among different web pages, you can use the Session object to store state between the group of related pages.  There are also situations where you may want to store information in the Session that is shared among ALL web pages in your ASP.NET application.  The ASP.NET Session object is another special object that is available to you in addition to the Response object that I covered in the previous lesson.  In this lesson I will show you an example of how you can use both of these special objects together to implement a two page credit card validation scenario.

The Session object is similar to a Dictionary object as it contains key/value pairs.  You can store as many variables in the Session as you need and they can be of any type, including your own User defined Class types.  When you store a variable in the Session, and you want to retrieve it back, use the same key value.  For example, if you store an object in the Session using the key “Myvar”, make sure you use “Myvar” when you want to retrieve the object back.

In the example for this lesson, I setup a web form called Page1 that contains some TextBox controls to allow the user to type their name and credit card information.  When the user clicks the Next button, I store the information entered into the Session object and redirect the application to another web form called Page2 (which is a confirmation page).

Session["FirstName"] = txtFirstName.Text;
Session["LastName"] = txtLastName.Text;
Session["CreditCardNumber"] = txtCreditCardNumber.Text;
Session["ExpirationDate"] = txtExpirationDate.Text;

Response.Redirect("Page2.aspx");

On the Page2 confirmation page, I read back the variables from the Session object and display them on the web page.

txtFirstName.Text = Session["FirstName"].ToString();
txtLastName.Text = Session["LastName"].ToString();
txtCreditCardNumber.Text = Session["CreditCardNumber"].ToString();
txtExpirationDate.Text = Session["ExpirationDate"].ToString();

Click here to watch an example video where I show you how I setup my web pages and implemented the Session variable storage and retrieval.

8 Comments »

  1. This is amazing

    Comment by Jay Maniar — July 23, 2009 @ 1:43 am

  2. This code is excellent…so easy to use…it worked instantly!

    Comment by Max452 — September 12, 2009 @ 4:22 am

  3. Its working fine in IE 7.0 and Mozilla. But not in IE6.0 . Plese give me any solution.

    Comment by Sathish — September 21, 2009 @ 8:37 am

  4. Even though iam a beginner , it is easy to me to understand the concept

    Comment by charles — November 3, 2009 @ 1:21 am

  5. Amazing.you have done a great job.

    Comment by charles — November 3, 2009 @ 1:22 am

  6. Hi, I want some more information which will help me in storing all the session variables in one class name session class… This informations are pretty good but expecting more…

    Comment by Bijal — December 29, 2009 @ 11:50 pm

  7. u r done a good job.

    Comment by Raghupal K — January 17, 2010 @ 6:10 am

  8. Great job.

    Comment by basant — January 19, 2010 @ 2:22 am

RSS feed for comments on this post. TrackBack URL

Leave a comment