Click here to Skip to main content
11,935,066 members (51,558 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked

Programming Practice for Server Side State Maintenance Variable

, 11 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Programming practice for server side state maintenance variable


Http protocol is stateless, so we need to maintain state on Client side or Server side. For this time, I am going to discuss about the maintaining state on Server Side with one programming practice.


To maintain state on Server side, most of the developers use Session, Caching, Application variables. But most of the beginner developers make one mistake, which is:

session["myData"] = data;

Code pushes data in session variable(s).

Data = (Conversion_To_Data_Type_Of_Data) session["mydata"];

Code gets data back from session variable.So most of the developers follow the above procedure to push and get data from server side variable.


  1. Developer requires remembering name of string value, i.e., name of the variable. If you refer to the above code, it's mydata.
  2. If there are a number of developers working on a project, it’s hard to maintain server side variable if we follow the above procedure, i.e., your maintenance increases.
  3. If there are large number of developers working in your project, you require to inform each and every one about the variable you are declaring.


The easiest solution that I found after doing so much programming is as follows:

Step 1

Create one static class:

public class SessionPropeties
 // code

Step 2

Create property for each session variable of your application as shown below:

public int UserId
            return Convert.ToInt32(HttpContext.Current.Session["UserID"].ToString());
          return 0;
            HttpContext.Current.Session["UserID"] = value;

After following the above steps, the class will be like below:

public class SessionPropeties
 public DataType prop1
            return Convert.ToDataType (HttpContext.Current.Session["prop1"].ToString());
            return defaultvalue;
            HttpContext.Current.Session["prop1"] = value;
 public DataType prop2
            return Convert.ToDataType (HttpContext.Current.Session["prop2"].ToString());
            return defaultvalue;
            HttpContext.Current.Session["prop2"] = value;

Step 3

In coding to get and set data, you just require to call these properties.

To set data
SessionProperties.UserId = 1;
To get data
int userid = SessionProperties.UserId;


  1. Once you declare your properties, you do not need to remember it. You just need to call your Session property class.
  2. Maintenance becomes easy when project size is large or there are a number of developers working in a team, because you just need to refer to the Sessionproperties class having all severside properties. And you can also add property you need and you don’t require to inform all the people working in your team.


You can maintain your Cache and Application server side variable in a similar way. So it's better you encapsulate your server side variable in one class which makes your task easy when you do code in web application.


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


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

GeneralAll objects into a single Class Pin
Yves17-Jan-11 15:59
memberYves17-Jan-11 15:59 
What just a simple Class with all the session members that we would need.
public class SessionPropeties
#region Public Data Member(s)

public int iUserID;
public string strLang;
public string strUserName;


#region Constructor

public SessionPropeties()
iUserID =-1;
strLang = "EN";
strUserName = "";


#region Public Method(s)

static SessionPropeties GetSession()
SessionPropeties mySession = null;
mySession = (SessionPropeties)HttpContext.Current.Session["MySessionProperties"];
catch { }
return mySession;

static void SaveSession(SessionPropeties _mySession)
HttpContext.Current.Session["MySessionProperties"] = _mySession;


You Build the object in Global.Asax

Sub Session_Start(ByVal Sender As Object, ByVal E As EventArgs)
Dim sessionInfo As New SessionPropeties()
'Do this only once
End Sub

You can access at all members like this ... StrongType properties
Dim sessionInfo As SessionPropeties= SessionPropeties.GetSession()
GeneralMy vote of 1 Pin
aprishchepov13-Jan-11 11:33
memberaprishchepov13-Jan-11 11:33 
GeneralRe: My vote of 1 Pin
Pranay Rana14-Jan-11 8:22
memberPranay Rana14-Jan-11 8:22 
GeneralMy vote of 4 Pin
Pravin Patil, Mumbai12-Jan-11 4:15
memberPravin Patil, Mumbai12-Jan-11 4:15 
GeneralRe: My vote of 4 Pin
Pranay Rana12-Jan-11 6:58
memberPranay Rana12-Jan-11 6:58 
GeneralMy vote of 4 Pin
Ruchit S.11-Jan-11 21:12
memberRuchit S.11-Jan-11 21:12 
GeneralRe: My vote of 4 Pin
Pranay Rana11-Jan-11 21:14
memberPranay Rana11-Jan-11 21:14 
GeneralGood job! Pin
Venkatesh Mookkan11-Jan-11 20:52
memberVenkatesh Mookkan11-Jan-11 20:52 
GeneralRe: Good job! Pin
Pranay Rana11-Jan-11 20:58
memberPranay Rana11-Jan-11 20:58 
GeneralMy vote of 4 Pin
Krunal Mevada11-Jan-11 19:48
memberKrunal Mevada11-Jan-11 19:48 
GeneralRe: My vote of 4 Pin
Pranay Rana11-Jan-11 21:06
memberPranay Rana11-Jan-11 21:06 
GeneralBetter design.. Pin
Ruchit S.11-Jan-11 19:43
memberRuchit S.11-Jan-11 19:43 
GeneralRe: Better design.. Pin
Venkatesh Mookkan11-Jan-11 20:57
memberVenkatesh Mookkan11-Jan-11 20:57 
GeneralRe: Better design.. Pin
Ruchit S.11-Jan-11 21:11
memberRuchit S.11-Jan-11 21:11 
GeneralThoughts Pin
PIEBALDconsult11-Jan-11 19:13
memberPIEBALDconsult11-Jan-11 19:13 
GeneralRe: Thoughts Pin
Pranay Rana11-Jan-11 19:33
memberPranay Rana11-Jan-11 19:33 
GeneralRe: Thoughts Pin
PIEBALDconsult12-Jan-11 3:28
memberPIEBALDconsult12-Jan-11 3:28 
GeneralRe: Thoughts Pin
Pranay Rana13-Jan-11 9:55
memberPranay Rana13-Jan-11 9:55 
GeneralRe: Thoughts Pin
Dstructr17-Jan-11 22:17
memberDstructr17-Jan-11 22:17 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 11 Jan 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid