4thSpace
4thSpace

Reputation: 44352

How to test for empty string on session object

I have the following test:

if(Session["mykey"] != string.Empty)
{...}

In VS.NET 2010's watch window, the value of Session["mykey"] is "". Yet I still go into the conditional. Do I need some other test?

--- EDIT --- After the above runs, I do reset the object by assigning string.Empty.

Upvotes: 0

Views: 121

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32511

try this

if(!string.InNullOrEmpty(Session["mykey"].ToString()))
{
    // some code
}

Upvotes: 4

Ghaleon
Ghaleon

Reputation: 1216

Of course, because you are comparing with the object of the Sessionsnot its text. Do like so:

if(Session["mykey"].ToString() != string.Empty)
{...}

other way to go is:

if(Session["mykey"] != null)
{...}

Upvotes: 4

Related Questions