How are you using session variables in Asp.net with C#.net

How are you using session variables in Asp.net with C#.net

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

  http://www.revenmerchantservices.com/post/2010/04/19/session-loss-in-webgarden.aspx

kick it on DotNetKicks.com

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

on page load

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

//  do soething

else
 
// 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.

e.g.

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

   {
 
       get
          {

              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

                }

              else
              { 
                     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?




Satalaj.

Tags:

Asp.net Session TimeoutIRequiresSessionState and sessionAsp.net SessionSessions In AspnetASP net Write Text On Image C#.net and Vb.net programAsp.net Session AbandonAsp.net Interview Questions for 2 years or 3 years experianceAsp.net delete file file on Server C#.net and VB.net codeSession application viewstateSession Loss In WebgardenHow to implement Captcha in Asp.net

Author

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 ASP.net 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.