Click here to Skip to main content
13,550,805 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 20 Jun 2012
Licenced CPOL

Session Management for Beginners

, 20 Jun 2012
Rate this:
Please Sign up or sign in to vote.
A simple idea to keep session variables easily accessible


Coming from an app development background, something I would do is create a class, say CurrentUser, and give a bunch of static variables that I need to access throughout the program. This then allows me to only fetch the data once, taking a tiny bit of pressure off of the server.

The problem with doing this in ASP is that the static variables will stored on the server, and anyone who logs in after the first person will just get their values instead of their own. Using Session there is a very easy, elegant work around. 

Using the code 

Start of lets create a CurrentUser class. Lets assume that throughout our application it is important to know their age, surname, and current city. We create these three variables as properties. Since we only need to retrieve the information, we only set a getter. 

The first thing we do is check whether our value exists in the session. If it is null, we call Initialize.

public class CurrentUser
    public static string Surname 
            if (String.IsNullOrEmpty(Session["surname"]))
            return (string)Session["surname"];
    public static string City
    public static string City
    private void Initialize()
        MyDataObject obj = FetchMyData();
        Session.Add("surname", obj.Surname);

That is pretty much it. It's not meant to be a super in depth article, just a little tip for you guys just starting on ASP. Hope you find it useful.


Version 1: Published.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Self Employed
South Africa South Africa
I've been passionate about coding since I can remember. My early years of coding involved QBasic and VB. My love for video games lured me into learning Game Maker and spent 2 years playing around with that. I have been programming in C# for a year now, and love learning new things.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Prasad_Kulkarni21-Jun-12 2:56
memberPrasad_Kulkarni21-Jun-12 2:56 
GeneralMy vote of 5 Pin
member6020-Jun-12 18:25
membermember6020-Jun-12 18:25 
my 5!

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 20 Jun 2012
Article Copyright 2012 by DominicZA
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid