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
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
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
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)
private static T GetFromSession<T>(string key)
{
object obj =HttpContext.Current.Session[key];
..
}
when try to get session value
any help would be greatly appreciated.