O'Reilly logo

ASP.NET 2.0 Cookbook, 2nd Edition by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.2. Maintaining Information About a User Throughout a Session

Problem

You want to make personalized information available to the users of your application for as long as each remains active without accessing a database each time the information is needed and regardless of the number of pages traversed.

Solution

Create a class in which to store the personalized data, instantiate the class and load the data, store the data object in the Session object, and access the data from the Session object as required.

In the code-behind class for your ASP.NET pages that need access to the data, use the .NET language of your choice to:

  1. Check to see if the object used to store the personalized data exists in the Session object.

  2. If the object exists, retrieve the object from Session. If the object does not exist, instantiate the class used for the personalized data and store it in the Session object.

  3. Use the data as required in your application.

A simple example that illustrates this solution is shown in Examples 7-8, 7-9, 7-10, 7-11 through 7-12. The example uses the class shown in Examples 7-8 (CH07PersonalDataVB for VB) and 7-9 (CH07PersonalDataCS for C#) to provide a container for some simple personalized data. This class contains properties for each of the data items and a default constructor.

Figure 7-2 shows a form that we’ve created for viewing the current contents of the personalized data stored in the Session object and for entering new session state data values. Example 7-10 shows the .aspx ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required