Checking Query String

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

To check if QueryString contains a key I compaire it with null like this
code:

if (Request.QueryString["id"]!=null)
{
Label1.Text= Request.QueryString["id"];
}
else
{
Label1.Text= "NoId";
}

Is it the best way to do see if QueryString contains a key or I am missing
some better method in QueryString?

Thanks,
Alan
 
No, that's pretty much it. You can change it to be a shorter using
something like:

Label1.Text = Request.QueryString["id"] == null ? "NoId" :
Requesty.QueryString["id"];
 
Thanks for help

So QueryString doesn't have any method for checking the existance of a key?

Alan


Marina said:
No, that's pretty much it. You can change it to be a shorter using
something like:

Label1.Text = Request.QueryString["id"] == null ? "NoId" :
Requesty.QueryString["id"];


A.M said:
Hi,

To check if QueryString contains a key I compaire it with null like this
code:

if (Request.QueryString["id"]!=null)
{
Label1.Text= Request.QueryString["id"];
}
else
{
Label1.Text= "NoId";
}

Is it the best way to do see if QueryString contains a key or I am missing
some better method in QueryString?

Thanks,
Alan
 
Have you looked at the .NET documentation? It fully documents all of this -
it is a lot faster to spend 1 minute reading the documentation, then waiting
for hours for someone to respond to your post. You would also probably end
up learning a lot of other useful things.

To answer your question, no there is not a method that will tell if a key
exists. There is a Keys collection - but you would have to loop through it
and check to see if the current key is the same as the one you are looking
for.

This is a lot more work then just checking if the value is null - which by
the way, is just as good a way to test if a key was passed in as anything
else.

A.M said:
Thanks for help

So QueryString doesn't have any method for checking the existance of a key?

Alan


Marina said:
No, that's pretty much it. You can change it to be a shorter using
something like:

Label1.Text = Request.QueryString["id"] == null ? "NoId" :
Requesty.QueryString["id"];


A.M said:
Hi,

To check if QueryString contains a key I compaire it with null like this
code:

if (Request.QueryString["id"]!=null)
{
Label1.Text= Request.QueryString["id"];
}
else
{
Label1.Text= "NoId";
}

Is it the best way to do see if QueryString contains a key or I am missing
some better method in QueryString?

Thanks,
Alan
 
Thank you for help.

Yes I did have a look at documentation and also searched couple of ASP.NET
portals.
I couldn't find anything.

Sometimes there are techniques that you can not find directly from
documentation. That is when you look for experienced people to ask them.

Alan



Marina said:
Have you looked at the .NET documentation? It fully documents all of this -
it is a lot faster to spend 1 minute reading the documentation, then waiting
for hours for someone to respond to your post. You would also probably end
up learning a lot of other useful things.

To answer your question, no there is not a method that will tell if a key
exists. There is a Keys collection - but you would have to loop through it
and check to see if the current key is the same as the one you are looking
for.

This is a lot more work then just checking if the value is null - which by
the way, is just as good a way to test if a key was passed in as anything
else.

A.M said:
Thanks for help

So QueryString doesn't have any method for checking the existance of a key?

Alan


Marina said:
No, that's pretty much it. You can change it to be a shorter using
something like:

Label1.Text = Request.QueryString["id"] == null ? "NoId" :
Requesty.QueryString["id"];


Hi,

To check if QueryString contains a key I compaire it with null like this
code:

if (Request.QueryString["id"]!=null)
{
Label1.Text= Request.QueryString["id"];
}
else
{
Label1.Text= "NoId";
}

Is it the best way to do see if QueryString contains a key or I am missing
some better method in QueryString?

Thanks,
Alan
 
Hello!
This is a lot more work then just checking if the value is null - which by
the way, is just as good a way to test if a key was passed in as anything
else.

I agree, but I would have liked to see the .Exists() or .Contains() method
as well.
 
Back
Top