Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello,

i am create a Property for session variable

C#
public static string CurrentFolderPath
       {
           get { return System.Web.HttpContext.Current.Session["FolderPath"].ToString(); }
           set { System.Web.HttpContext.Current.Session["FolderPath"] = value; }
       }


but when i am checking the value then it through error Object reference not set to an instance of an object

C#
if (SessionManager.CurrentFolderPath != string.Empty)
              {
                  path = CommonFunctions.UserFolder;
              }


it Through error. Please Help me How to Check This variable value.
Posted

1 solution

you may need to add null check in getter of the property

C#
public static string CurrentFolderPath
{
    get
    {
        if (HttpContext.Current.Session["FolderPath"] == null)
            return string.Empty;
        else
            return (string)HttpContext.Current.Session["FolderPath"];
    }

    set
    {
        HttpContext.Current.Session["FolderPath"] = value;
    }
}


side note: to check string empty you can use string.IsNullOrEmpty method
C#
if (!string.IsNullOrEmpty(SessionManager.CurrentFolderPath))
{
   path = CommonFunctions.UserFolder;
}
 
Share this answer
 
v3
Comments
Yogesh Kumar Tyagi 28-Apr-14 3:38am    
Thanks Damith Weerasinghe

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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