Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i am using session helper class to manage session,because i had many session variables and it was messy to use hardcoded session everywhere,below is the class i have used

C#
public static class Sessionhelper
{
    private static T GetFromSession<T>(string key)
    {
        object obj =HttpContext.Current.Session[key];
        if (obj == null)
        {
            return default(T);
        }
        return (T)obj;
    }

    private static void SetInSession<T>(string key, T value)
    {
        if (value == null)
        {
            HttpContext.Current.Session.Remove(key);
        }
        else
        {
            HttpContext.Current.Session[key] = value;
        }
    }


and the properties to access them
C#
public static string CustomerName
   {
       get
       {
           if (string.IsNullOrEmpty(GetFromSession<string>("Customerselected")))
           {
               HttpContext.Current.Response.Redirect("~/My_App/MYUI/Home.aspx", false);
               return GetFromSession<string>("Customerselected");
           }
           else
           {
               return GetFromSession<string>("Customerselected");
           }
       }
       set { SetInSession<string>("Customerselected", value); }
   }

   public static string fid
   {
       get { return GetFromSession<string>("fid"); }
       set { SetInSession<string>("fid", value); }
   }


1)if the customerselected is null i am redirecting to home page, is it thread safe to do,and correct way?
2)i can save value to session using
C#
Sessionhelper.UserId = userid.ToString();


but i get null reference exception was unhandled by user code( object reference was not set to instance of an object)

C#
 private static T GetFromSession<T>(string key)
    {
        object obj =HttpContext.Current.Session[key];//here i get that exception
..
}


when try to get session value


any help would be greatly appreciated.
Posted

Could you check your session. does it get expire? Because even if Session key is not found, you s'd not get null exception error.
 
Share this answer
 
v2
Comments
girish sp 28-Sep-12 5:50am    
to test the same i just inserted some value to session and then tried to retrieve,

but the thing you said is correct "if Session key is not found, you s'd not get null exception error."
You could possibly do something like this, to check the key exists first...

C#
public static T Get<T>(string key)
{
    if (Session[key] == null)
        return default(T);
    else
        return (T)Session[key];
}


Have a look at this thread, which shows a similar technique to you

http://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c[^]

Also, don't mix your 'Session state management' and your 'application' logic, i.e. you are redirecting from the session state manager. Have whatever accesses that, manange the redirect

e.g.
C#
if (string.IsNullOrEmpty(Sessionhelper.UserName))
    HttpContext.Current.Response.Redirect("~/My_App/MYUI/Home.aspx", false);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900