0

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?

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
  • Seems that this question may have already been answered: [What is the best way to determine a session variable is null or empty in C#?](https://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c?rq=1) – MJ713 Feb 01 '17 at 21:02

2 Answers2

3

You can use IsNullOrEmpty from string.

if (string.IsNullOrEmpty((string)Session["PlanID"])) {
   string PlanID = Request.QueryString["PlanID"];
   Session["PlanID"] = PlanID;
}
  • And, depending on .net version, `String.IsNullOrWhitespace` may be available (and preferable). – Brad Christie Feb 01 '17 at 21:01
  • I'd recommend `string.IsNullOrWhitespace` over `IsNullOrEmpty` as it covers more of the "undesirable" spectrum. – Theo Feb 01 '17 at 21:01
  • IsNullOrWhitespace may be preferable, but he said "empty space" which is why I answered with IsNullOrEmpty. If you want to check null, "", or " ", then yes, use IsNullOrWhitespace. – John-Paul Ensign Feb 01 '17 at 21:03
1

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;
}
Grizzly
  • 5,873
  • 8
  • 56
  • 109