Johnny Bones
Johnny Bones

Reputation: 8404

Testing a session variable for null or empty space

What I currently have is this:

if ((string)Session["PlanID"] == null)
{
   string PlanID = Request.QueryString["PlanID"];
   Session["PlanID"] = PlanID;
}

What I need is something like this:

if ((string)Session["PlanID"] == null) or if ((string)Session["PlanID"] == "")
{
   string PlanID = Request.QueryString["PlanID"];
   Session["PlanID"] = PlanID;
}

How would I do that?

Upvotes: 0

Views: 399

Answers (2)

Grizzly
Grizzly

Reputation: 5953

You can use the String.IsNullOrWhiteSpace Method.

This method also checks for null values

Indicates whether a specified string is null, empty, or consists only of white-space characters.

if (string.IsNullOrWhiteSpace((string)Session["PlanID"]))
{
   string PlanID = Request.QueryString["PlanID"];
   Session["PlanID"] = PlanID;
}

Upvotes: 1

John-Paul Ensign
John-Paul Ensign

Reputation: 141

You can use IsNullOrEmpty from string.

if (string.IsNullOrEmpty((string)Session["PlanID"])) {
   string PlanID = Request.QueryString["PlanID"];
   Session["PlanID"] = PlanID;
}

Upvotes: 3

Related Questions