user2147163
user2147163

Reputation: 87

How to Check If QueryString has no parameter

I am facing one issue in Querystring when I don't have any parameters.Please find the below example. I have a below URL

1 Scenario

URL ---http://localhost/Employee/Emp/empmanagement.aspx

and I'm checking one condition and it is throwing error Request is not available

if(Request.QueryString.ToString().Contains("employeeData"))

2 Scenario

URL ---http://localhost/Employee/Emp/empmanagement.aspx?empData=employeeData

and it is working fine below

if(Request.QueryString.ToString().Contains("employeeData"))

Thanks Guys everyone's answer is correct the issue was because of my context.Qerystring was not returing.So,i declared in my aspx page and it is working fine for me.

ASPX Code

 <cw:QueryString runat="server" ID="_empValue"  Required="False" />

Code Behind Code

if(_empValue.Value != null && _empValue.Value.Contains("employeeData")

Upvotes: 3

Views: 5990

Answers (5)

user2147163
user2147163

Reputation: 87

Thanks Guys, everyone's answer is correct the issue was because of my context query string was not returning sometimes.

So, I declared in my aspx page and it is working fine for me.

ASPX markup:

<cw:QueryString runat="server" ID="_empValue"  Required="False" />

Code-behind:

if(_empValue.Value != null && _empValue.Value.Contains("employeeData")

Upvotes: 0

Dot_NET Pro
Dot_NET Pro

Reputation: 2123

Try this:

if(Request!=null && Request.QueryString.Keys.Count > 0)
{
     if(Request.QueryString.ToString().Contains("employeeData"))
     {
     }
}

Upvotes: 0

Rahul Nikate
Rahul Nikate

Reputation: 6337

This should be enough

if(Request != null && Request.QueryString["employeeData"] != null)
{
}

OR

if (Request != null && Request.QueryString.Keys.Count > 0)
{
}

OR

if (Request != null && string.IsNullOrEmpty(Request.QueryString["employeeData"]))
{

}

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172638

You can try

if (Request.QueryString.Keys.Count > 0)
{

}

or you can try

if(Request.QueryString.AllKeys.Any(i => i == "query"))

Upvotes: 0

Imad
Imad

Reputation: 7490

Request.QueryString is nothing but a NameValueCollection i.e. one of collection. So like other collections it also has Count property. So you can check

Request.QueryString.Keys.Count > 0

Upvotes: 0

Related Questions