I would recommend doing the following.
if (Request.QueryString["Sno"] == null || Request.QueryString["Name"] == null)
{
lblBookedBy.Text = "";
lblSno.Text = "";
}
else
{
lblBookedBy.Text = Request.QueryString["Name"].ToString();
lblSno.Text = Request.QueryString["Sno"].ToString();
}
You are most likely getting a NullReference
in the if
statement. This way you are sure not to encounter this, and worst case if both of the variables are instantiated, but one or more contains an empty string it will simply set the Text
to empty
.
Alternatively if you use Convert.ToString
as many other suggested you can simplify the code by skipping the if
statement.
lblBookedBy.Text = Convert.ToString(Request.QueryString["Name"]);
lblSno.Text = Convert.ToString(Request.QueryString["Sno"]);
In worst case scenario one of these will be Null
, and will result in one of the TextBoxes to show result, while the other one is empty.
Also, assuming that Request.QueryString
supports it, you could use TryGetValue.