0

I am trying to check if a Session state variable is null or no. If it is not null there is a function to be used. However this is throwing an exception.

When Session["Set_Parameters] is null it is throwing an exception. I need it to avoid the if statement if it is null.

if(!string.IsNullOrEmpty(Session["Set_Parameters"].ToString()))
        {
            Edit_String();
        } 

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Web.SessionState.HttpSessionState.this[string].get returned null.

This is the exception I am getting.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Parth
  • 1
  • 1

1 Answers1

-2

Problem with your code is you're trying to convert a null value to string, then test that for null or empty.

Try this...

If (Session["Set_Parameters"] != null) {
  ...
}
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
tcoffsite
  • 1
  • 3