Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Check query string in one line

5.00/5 (1 vote)
22 Jun 2010CPOL 10.5K  
Hello friends
Normally we check query string by this way

if(Request.QueryString["Item"] != null)
{
myItem = Request.QueryString["Item"].ToString() ;
Label1.Text = myItem ;
}
else
{
Label1.Text = "none";
}


We write above code in online like this.

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Item"] != null?
Request.QueryString["Item"].ToString() : "none";
}



Even shorter way of writing the code:

Label1.Text = Request.QueryString["Item"] ?? "none";

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)