Avatar of Camillia
Camillia
Flag 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

ASP.NET

Avatar of undefined
Last Comment
Camillia

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kyle Abrahams

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Daniel Van Der Werken

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck