Home Page

UPDATE: 2010 Developer Action Plan The folks at LearnVisualStudio.NET have created a new Developer Action Plan packed with strategies and advice to help you get that elusive first .NET development job and also become an awesome ASP.NET developer. This action plan is perfect:

  • For those with little or no .NET experience, or...
  • For those with programming experience, but who feel like their skills are deteriorating or becoming stale

If you simply enter your email in the next 15 minutes, you will get:

  • A download link for the 2010 Developer Action Plan
  • 4 hours of free ASP.NET training videos you can watch from the comfort of your computer
==> Click HERE to get your gifts immediately. It's safe and easy. <==


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.

12 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

  9. Good concept

    Comment by sudhir — February 5, 2010 @ 5:10 am

  10. I am developing a project, in which i created one page called menu.aspx and I dragged a checkboxlist in that,
    I need an answer for,
    When i select one or more than one item from that checkedlistBox , i need the value of selected items in a session variable i.e how to store an array of Selected item from a checkedListBox into a session variable and how to retrive it in another page.
    please Answer me.

    Comment by samina — March 26, 2010 @ 6:12 am

  11. well done bro

    Comment by luis — March 26, 2010 @ 2:10 pm

  12. a good code for creating session in asp.net c#

    Comment by rishab aggarwal — March 30, 2010 @ 9:53 am

RSS feed for comments on this post. TrackBack URL

Leave a comment