Sunday, December 13, 2009

AVOID ASP.net SESSION ERROR..(null reference and others...!)

here goes our session wrapper class.....
public static class SessionHandler
{

private static string _userCountryKey = "UserCountry";

public static string UserCountry
{

get
{

/
if (HttpContext.Current.Session[SessionHandler._userCountryKey] == null)
{

return string.Empty;
}
else
{
return HttpContext.Current.Session[SessionHandler._userCountryKey].ToString();
}

}

set
{
HttpContext.Current.Session[SessionHandler._userCountryKey] = value;
}

}

}


here we can set and retrive it ....

SessionHandler.UserCountry="INDIA"

retrive using

Response.Write(SessionHandler.UserCountry);


Avoid all null references that are eating us ......