3

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")
6
  • 1
    Have you ever check it's Count property like Request.QueryString.Count > 0? Commented Dec 28, 2015 at 11:03
  • Hi Soner,Actually in above first scenario there are no parameters so even Request is not coming.it is throwing error Request is not available in this context Commented Dec 28, 2015 at 11:05
  • Where do you write those lines exactly? Which file? Commented Dec 28, 2015 at 11:07
  • @SonerGönül do you think .ToString() is the culprit in the first line? Commented Dec 28, 2015 at 12:23
  • @शेखर It can be. Since QueryString returns NameValueCollection, calling ToString calls object.ToString and this might behave differently for it. I don't know honestly. I do not have a compiler right now. Commented Dec 28, 2015 at 12:31

5 Answers 5

3

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"]))
{

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

You can try

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

}

or you can try

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

Comments

0

Try this:

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

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.