Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

string.IsNullOrEmpty throws object reference is null error

I have the code below. Shouldnt string.IsNullOrEmpty catch if the value is null or empty? It throws an error that the object reference is not set or null. How can I recode this?

get
        {
             if (string.IsNullOrEmpty(Session["BusinessId"].ToString())) //this line
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
            return int.Parse(Session["BusinessId"].ToString());
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

If i use
protected override void OnInit()
{
   Session["BusinessID"] = string.Empty;
}

I need to add it to every page? because I check that session in every page.