Programming Practice for Server Side State Maintenance Variable in ASP.NET

No.of Views902
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  10 Jan 2011 19:01:07
Tag : ASP.NET , State Management
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

Problem

To maintain state on Server side most of the developer use Session, Caching, Application variables. But most of the beginner developer does 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 above procedure to push and get data from server side variable.

Disadvantages

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

Solution

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

Step 1

Create one Static class

public class SessionPropeties
{
 // code
}

Step 2

Create property for each session variable of your application as show below

public int UserId
{
        get
        {
           if(HttpContext.Current.Session["UserID"]!=null)
            return Convert.ToInt32(HttpContext.Current.Session["UserID"].ToString());
           else
          return 0;
        }
        set
        {
            HttpContext.Current.Session["UserID"] = value;
        }
}

After following above steps class will be like as below

public class SessionPropeties
{
 public DataType prop1
 {
        get
        {
           if(HttpContext.Current.Session["prop1"]!=null)
            return Convert.ToDataType (HttpContext.Current.Session["prop1"].ToString());
           else
            return defaultvalue;
        }
        set
        {
            HttpContext.Current.Session["prop1"] = value;
        }
      
 }
 public DataType prop2
 {
        get
        {
           if(HttpContext.Current.Session["prop2"]!=null)
            return Convert.ToDataType (HttpContext.Current.Session["prop2"].ToString());
           else
            return defaultvalue;
        }
        set
        {
            HttpContext.Current.Session["prop2"] = value;
        }
      
 }
 
 ...........
 
}

Step 3
In coding to get and set data just require to call this properties
To set data

SessionProperties.UserId = 1;

To get data

int userid = SessionProperties.UserId;

Advantages

  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 no. of developer working in team, because you just need to refer Sessionproperties class having all severside properties. And you can also add property you need and you don’t require informing all the people working in your team.

Summary

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

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 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 me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    Cookie is one of several ways to store data about web site visitors during the time when web server and browser are not connected. They are small files That are created on the client’s hard drive (or, if they’re temporary, in the web browser’s memory).
    Published Date : 02/Jun/2011
    It is a storage mechanism that is accessible from all pages requested by a single Web browser session. Therefore, you can use session state to store user-specific information. A session is defined as the period of time that a unique user interacts with a Web application. Active Server Pages (ASP) developers who wish to retain data for unique user sessions can use an intrinsic feature known as session state.
    Published Date : 02/Jun/2011
    It store global objects that can be accessed by any client.It supports the same types what session state support which are of type objects, retains information on the server, and uses the same dictionary-based syntax. Application state is based on the System.Web.HttpApplicationState class, which is provided in all web pages through the built-in Application object.
    Published Date : 09/May/2011
    Now web application framework, no matter how advanced, can change that HTTP is a stateless protocol. So inherently we should also forget our users, but unfortunately we cannot.ASP.Net Framework provides us features by which we can maintain states of your users.
    Published Date : 26/Jun/2010
    There is Lots of ways and Techniques Are available to Manage you State on Web in Aspnet
    Published Date : 06/May/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top