Check if query string variable exists ASP.NET C#


This is something to do with the Request.QueryString () function.
You can use the following piece of code to check the existence of a query string variable.

 if (Request.QueryString["[VariableName]"] != "")
        {
            // Do something
        } 


One of the problem with the above query is ,if the variable was not present in the query string ,the If statement will return a true and the code inside the IF will get executed .

So we need to use the following code to overcome this .

if (Request.QueryString["[Variable Name]"] != "" && Request.QueryString["[VariableName]"] != null
{   
     // do something
 

This code will check the existence of the variable name specified in the query string  and also if it contains a value or not. 

Hope this helps !

3 comments: