Click here to Skip to main content
11,577,965 members (62,996 online)
Click here to Skip to main content

Create Typed Session State in ASP.NET

, 8 Jun 2002 105.3K 43
Rate this:
Please Sign up or sign in to vote.
Wrap the Session object in ASP.NET to prevent programming errors and keep code clean

Having a strongly-typed object or interface to develop against is generally much easier than dealing with loosely-typed interfaces. Many common errors can be caught at compile time rather than at run time, which hastens development time.

One of the things that has bothered me about the Session object in ASP.NET is that it is loosely typed, returning everything as the Object base type. Equally bothersome, the Session object requires you to pass in a string key to obtain the object you want. I don’t know how many times I’ve forgotten what key I was using for a piece of data and had to look through my code to find it. Maintaining a data dictionary in a separate document helped, but I would sometimes start using a new piece of data and forget to update my dictionary!

After having an application developed where I had the Session object sprinkled throughout my code I realized that I could create an object to wrap the Session object and clean up my code. It looks something like this:

public class State {
	public static int CustomerId{
		get{ return (int)HttpContext.Current.Session["CustomerId"]; }
		set{ HttpContext.Current.Session["CustomerId"] = value; }

Now code that once appeared as:

DataTable tbl = GetInvoices((int)Session[“CustomerId”]);

now looks like this:

DataTable tbl = GetInvoices(State.CustomerId);

It takes a little bit of work to setup each session variable but once you try it I think you’ll find your code is a whole lot cleaner and bug free.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Scott Juranek
Chief Technology Officer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralhttpContext not declared Pin
Anonymous14-Mar-04 13:55
sussAnonymous14-Mar-04 13:55 
GeneralRe: httpContext not declared Pin
pmpjr16-Aug-05 16:49
memberpmpjr16-Aug-05 16:49 
Generalclean the data in session Pin
CZIU11-Mar-03 10:50
memberCZIU11-Mar-03 10:50 
GeneralRe: clean the data in session Pin
ZackJones30-Sep-03 15:23
memberZackJones30-Sep-03 15:23 
GeneralGood idea but how about autogenerating Pin
Rama Krishna10-Jun-02 12:41
memberRama Krishna10-Jun-02 12:41 
GeneralRe: Good idea but how about autogenerating Pin
Scott Juranek10-Jun-02 13:33
memberScott Juranek10-Jun-02 13:33 
GeneralRe: Good idea but how about autogenerating Pin
Rama Krishna10-Jun-02 14:00
memberRama Krishna10-Jun-02 14:00 
GeneralRe: Good idea but how about autogenerating Pin
Scott Juranek10-Jun-02 14:15
memberScott Juranek10-Jun-02 14:15 
For example, take a web application where all users from the same organization have certain global settings.   Instead of putting these shared settings into each person's session I can load this into the Cache.   Then each person's session has the organization they are associated with and I can use that to locate the org data in the Cache.   For example:

public class State {

   public static int OrgId {
         // get & set work from Session

   public static OrganizationInfo OrgInfo {
         get {
            // use OrgId property to construct cache key
            // locate OrganizationInfo in cache and return


My original intent was to just wrap the Session object but I've found that certain 'session' data would be best located in the Cache so there are a few times when I've used the above technique.
GeneralAlternative Idea Pin
Anonymous10-Jun-02 5:43
memberAnonymous10-Jun-02 5:43 
GeneralRe: Alternative Idea Pin
Günther Makulik10-Jun-02 12:08
memberGünther Makulik10-Jun-02 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 9 Jun 2002
Article Copyright 2002 by Scott Juranek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid