How are you using session variables in with

How are you using session variables in with

  If you are loosing values stored inside session, below URL will tell you the possible scenarios where this can happen.

kick it on

    Many times I had seen developers are using session in like this.

on page load

if (session["User"] != null)

//  do soething

// do some

This becomes nightmare to debug or manage session objects as you have created session as per your requirement. Its nightmare to read the code.

For best practice follow below steps while working with Session objects.

Create a class, name it as XYZSessionManager. where prefix XYZ can be your comapny name or DB name.


Lest create UserInfo entity which will hold FirstName, LastName, properties of User.

public class UserInfo
    private string _firstName;
    public string FirstName
        get {return _firstName;}
        set {_firstName = value;}
  private string _lastName;
   public string LastName
     get{return _lastName;}
     set{_lastName = value;}

Lets add this into session.

 public class XYZsessionManager

   public static UserInfo GetUserInfo


              if( HttpContext.Current.Session["User"] != null)  // Check wheather session object exists or not              

                     return (UserInfo)HttpContext.Current.Session["User"] ; // if exists unbox it into UserInfo object


                     UserInfo u  = New UserInfo();  // Instantiate
                     u = XYZController.Provider.GetUserInfo();
                       // XyzController.provider.GetUserInfo will talke to your Db and it will populate the UserInfo entity
                     HttpContext.Current.Session["User"]     = u;

                       //  add UserInfo object u into Session.

                     return u; 
                       // don't cast the session into UserInfo and return. just return u . Avoid casting (Boxing / unboxing) .                     




ok we have done with the XYZ session manger. we can do similar steps for other session variable or Cache variables.

Now, How to access it into your code?

  Wrong way :

UserInfo u = new UserInfo();                         // No need to instantiate

u = XyzController.GetUserInfo();                     // this will request session

Correct way

UserInfo u = XyzController.GetUserInfo();      // without creating new instance of an object UserInfo, here we are just refering the refrence to allready created instance.

There is no need to create new copy of object who looks like UserInfo and refer it to another new instance. Are you getting my point?


Tags: Session TimeoutIRequiresSessionState and SessionSessions In AspnetASP net Write Text On Image and Session Interview Questions for 2 years or 3 years delete file file on Server and codeSession application viewstateSession Loss In WebgardenHow to implement Captcha in


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.