Click here to Skip to main content
13,147,689 members (30,552 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 12 Jun 2014

Session Variables Management

, 12 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Session variables management

Session variables are used in our web applications quite often. Some of the variables are maintained for the whole session so that we can use them in different places to perform application’s logic. But while getting some values, errors happen just because of a small silly spelling mistake and application blows up. We can somehow overcome most of the common session variables spelling mistake problems by using static classes. Because in this way, you don’t need to spell each time you access.

In this post, I created a static class in Web/UI layer and created properties for all those common session variables used by our application. So in this way, you can set or get your session variables just using this class. It also serves you to organize your session variables in your project.

As it is only a class, I captured it in an image, but if you need the source code, it can be provided.

/// <summary>
   /// Managing Session Variables
   /// </summary>
   internal static class BlogSessionVariables
       /// <summary>
       /// Gets sets UserIdentification variable.
       /// </summary>
       public static string UserIdentifier
           get { return (HttpContext.Current.Session["UserIdentification"] != null ?
          Convert.ToString(HttpContext.Current.Session["UserIdentification"]) : string.Empty); }
           set { HttpContext.Current.Session["UserIdentification"] = value; }

       /// <summary>
       /// Gets sets articles variable.
       /// </summary>
       public static List<dynamic> Articles
           get { return (HttpContext.Current.Session["Articles"] != null ?
           (List<dynamic>)HttpContext.Current.Session["Articles"] : null); }
           set { HttpContext.Current.Session["Articles"] = value; }

Click the image to enlarge so you can view the whole class and test functions.

session variable management


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


About the Author

Software Developer (Senior)
United States United States
My name is Muhammad Hussain. I'm a Software Engineer.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 13 Jun 2014
Article Copyright 2014 by M.Hussain.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid