Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET
Tip/Trick

Check query string in one line

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Jun 2010CPOL 9.9K   2   3
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)


Written By
Software Developer (Senior) Wipro
India India
Asp.net,Ajax,Webparts,Web service,Window application,Window service,Sql Server

Comments and Discussions

 
QuestionReply to Format for QueryString in asp.net Pin
Vaigai Natarajan22-Oct-13 19:07
professionalVaigai Natarajan22-Oct-13 19:07 
GeneralDude, add code formats to this Tip/Trick. Pin
thatraja29-Oct-10 20:49
professionalthatraja29-Oct-10 20:49 
GeneralNice one, thx Pin
mario_kovac6-Jul-10 2:35
mario_kovac6-Jul-10 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.