QueryString keys

A

Alex

I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1. QueryString["client"] is null, QueryString.Keys[0] is null, QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no value assigned to it. Any ideas?

Thanks,

Alex
 
M

Mythran

Private Function KeyInQuery(ByVal Key As String) As Boolean
Dim keyName As String
Dim val As String

For i As Integer = 0 To Request.QueryString.Count - 1
val = Request.QueryString.Item(i)

If Not val Is Nothing AndAlso val.ToLower() = Key.ToLower()
Return True
End If
Next

Return False
End Function

Try the above function :)

Mythran
I have a question about determining if one QueryString keys exists. The idea
is, if this key exists, than its presence is enough to indicate that its value is
true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that
client exists, so its value is true. In the code behind, I can tell that client
exists using Request.QueryString.Keys.Count, which equals 1.
QueryString["client"] is null, QueryString.Keys[0] is null,
QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no
value assigned to it. Any ideas?

Thanks,

Alex
 
G

Guest

I'm not sure I understand what the dilema is. If you can tell from Keys.Count
= 1 that the querystring has atleast one key and you can reference
QueryString["Client"] and determine if it is null or not then what is the
problem?

Besides, what good is a QueryString key with no value?

I think somehow your approach to produce a solution is flawed. Perhaps, more
information about what it is you are actually trying to accomplish? A little
more of a bigger picture?

-Demetri
 
G

Greg Burns

Besides, what good is a QueryString key with no value?

I think he said he wants to use the key as a boolean flag. I've ran into
this myself before. Seems silly to pass a qs like client=true or
client=false, or client=1 and client=0, etc. When just passing client by
itself could represent "true" and no qs could represent "false".

my .02
Greg


Demetri said:
I'm not sure I understand what the dilema is. If you can tell from
Keys.Count
= 1 that the querystring has atleast one key and you can reference
QueryString["Client"] and determine if it is null or not then what is the
problem?

Besides, what good is a QueryString key with no value?

I think somehow your approach to produce a solution is flawed. Perhaps,
more
information about what it is you are actually trying to accomplish? A
little
more of a bigger picture?

-Demetri

Alex said:
I have a question about determining if one QueryString keys exists. The
idea is, if this key exists, than its presence is enough to indicate that
its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me
that client exists, so its value is true. In the code behind, I can tell
that client exists using Request.QueryString.Keys.Count, which equals 1.
QueryString["client"] is null, QueryString.Keys[0] is null,
QueryString.Keys["client"] is null, Request.QueryStingText = "client",
ect.

I cannot seem to find a way to determine if a QuerySting exists when it
has no value assigned to it. Any ideas?

Thanks,

Alex
 
A

Alex

Greg,

That is exactly what my aim is. Thanks for your input.

Akex

--
Alex Mueller
Greg Burns said:
Besides, what good is a QueryString key with no value?

I think he said he wants to use the key as a boolean flag. I've ran into
this myself before. Seems silly to pass a qs like client=true or
client=false, or client=1 and client=0, etc. When just passing client by
itself could represent "true" and no qs could represent "false".

my .02
Greg


Demetri said:
I'm not sure I understand what the dilema is. If you can tell from
Keys.Count
= 1 that the querystring has atleast one key and you can reference
QueryString["Client"] and determine if it is null or not then what is the
problem?

Besides, what good is a QueryString key with no value?

I think somehow your approach to produce a solution is flawed. Perhaps,
more
information about what it is you are actually trying to accomplish? A
little
more of a bigger picture?

-Demetri

Alex said:
I have a question about determining if one QueryString keys exists. The
idea is, if this key exists, than its presence is enough to indicate that
its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me
that client exists, so its value is true. In the code behind, I can tell
that client exists using Request.QueryString.Keys.Count, which equals 1.
QueryString["client"] is null, QueryString.Keys[0] is null,
QueryString.Keys["client"] is null, Request.QueryStingText = "client",
ect.

I cannot seem to find a way to determine if a QuerySting exists when it
has no value assigned to it. Any ideas?

Thanks,

Alex
 
A

Alex

Ok, just for this scenario, the only QueryString key on the url is client, as in www...com/main.aspx?client. QueryString["client"] will return null. QueryString.Keys.Count will return 1. I have no way of telling that the QS client is present b/c it does not have a value assigned to it. I could write a function to search parts of the url, but I wanted to see if any answers would surface first.

I do not have a QueryString.Item in C#. I do have other name value collections, but none of them work for me as I reported in the original post. Any more ideas?

Thanks

Alex
Private Function KeyInQuery(ByVal Key As String) As Boolean
Dim keyName As String
Dim val As String

For i As Integer = 0 To Request.QueryString.Count - 1
val = Request.QueryString.Item(i)

If Not val Is Nothing AndAlso val.ToLower() = Key.ToLower()
Return True
End If
Next

Return False
End Function

Try the above function :)

Mythran
I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1. QueryString["client"] is null, QueryString.Keys[0] is null, QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no value assigned to it. Any ideas?

Thanks,

Alex
 
G

Guest

Request.QueryString.ToString() will return "Client"

Perhaps, you can split the querystring into a string array variable and do a
foreach on the string array and test for the string "Client" to see if it
exist.

-Demetri

Alex said:
Greg,

That is exactly what my aim is. Thanks for your input.

Akex

--
Alex Mueller
Greg Burns said:
Besides, what good is a QueryString key with no value?

I think he said he wants to use the key as a boolean flag. I've ran into
this myself before. Seems silly to pass a qs like client=true or
client=false, or client=1 and client=0, etc. When just passing client by
itself could represent "true" and no qs could represent "false".

my .02
Greg


Demetri said:
I'm not sure I understand what the dilema is. If you can tell from
Keys.Count
= 1 that the querystring has atleast one key and you can reference
QueryString["Client"] and determine if it is null or not then what is the
problem?

Besides, what good is a QueryString key with no value?

I think somehow your approach to produce a solution is flawed. Perhaps,
more
information about what it is you are actually trying to accomplish? A
little
more of a bigger picture?

-Demetri

:

I have a question about determining if one QueryString keys exists. The
idea is, if this key exists, than its presence is enough to indicate that
its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me
that client exists, so its value is true. In the code behind, I can tell
that client exists using Request.QueryString.Keys.Count, which equals 1.
QueryString["client"] is null, QueryString.Keys[0] is null,
QueryString.Keys["client"] is null, Request.QueryStingText = "client",
ect.

I cannot seem to find a way to determine if a QuerySting exists when it
has no value assigned to it. Any ideas?

Thanks,

Alex
 
G

Greg Burns

QueryString("client") does indeed return a value of Nothing (in VB), so that won't help you. But you can search to see if the key exists in the collection...

This should work. BTW, it is essential the same as Mythran posted previously.

Dim found As Boolean = False
For i As Integer = 0 To Request.QueryString.Count - 1
If Request.QueryString(i).ToLower = "client" Then
found = True
Exit For
End If
Next

Greg

Ok, just for this scenario, the only QueryString key on the url is client, as in www...com/main.aspx?client. QueryString["client"] will return null. QueryString.Keys.Count will return 1. I have no way of telling that the QS client is present b/c it does not have a value assigned to it. I could write a function to search parts of the url, but I wanted to see if any answers would surface first.

I do not have a QueryString.Item in C#. I do have other name value collections, but none of them work for me as I reported in the original post. Any more ideas?

Thanks

Alex
Private Function KeyInQuery(ByVal Key As String) As Boolean
Dim keyName As String
Dim val As String

For i As Integer = 0 To Request.QueryString.Count - 1
val = Request.QueryString.Item(i)

If Not val Is Nothing AndAlso val.ToLower() = Key.ToLower()
Return True
End If
Next

Return False
End Function

Try the above function :)

Mythran
I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1. QueryString["client"] is null, QueryString.Keys[0] is null, QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no value assigned to it. Any ideas?

Thanks,

Alex
 
B

bruce barker

the problem you have is the query string is not well formed, the "=" is required by the w3c standard for it to be a valid url. but all is not lost, try

Page.Request.ServerVariables["QUERY_STRING"]

this will return the raw querystring

-- bruce (sqlwork.com)


I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1. QueryString["client"] is null, QueryString.Keys[0] is null, QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no value assigned to it. Any ideas?

Thanks,

Alex
 
M

Mythran

How could you not have the QueryString.Item property??? It's the same object
collection in VB.Net as it is in C#....anywho, what my routine does is looks for
the VALUE named "client" and not the KEY. That is why QueryString(Key) gives you
a value of Nothing. There is no KEY called "client", but there is a value called
"client" that is not associated with a key (the key itself is "" or Nothing).

Try QueryString instead of QueryString.Item. Maybe that will work :)

Mythran

QueryString("client") does indeed return a value of Nothing (in VB), so that
won't help you. But you can search to see if the key exists in the collection...

This should work. BTW, it is essential the same as Mythran posted previously.

Dim found As Boolean = False
For i As Integer = 0 To Request.QueryString.Count - 1
If Request.QueryString(i).ToLower = "client" Then
found = True
Exit For
End If
Next

Greg

Ok, just for this scenario, the only QueryString key on the url is client, as
in www...com/main.aspx?client. QueryString["client"] will return null.
QueryString.Keys.Count will return 1. I have no way of telling that the QS client
is present b/c it does not have a value assigned to it. I could write a function
to search parts of the url, but I wanted to see if any answers would surface
first.

I do not have a QueryString.Item in C#. I do have other name value
collections, but none of them work for me as I reported in the original post. Any
more ideas?

Thanks

Alex
Private Function KeyInQuery(ByVal Key As String) As Boolean
Dim keyName As String
Dim val As String

For i As Integer = 0 To Request.QueryString.Count - 1
val = Request.QueryString.Item(i)

If Not val Is Nothing AndAlso val.ToLower() = Key.ToLower()
Return True
End If
Next

Return False
End Function

Try the above function :)

Mythran
I have a question about determining if one QueryString keys exists. The
idea is, if this key exists, than its presence is enough to indicate that its
value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me
that client exists, so its value is true. In the code behind, I can tell that
client exists using Request.QueryString.Keys.Count, which equals 1.
QueryString["client"] is null, QueryString.Keys[0] is null,
QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it
has no value assigned to it. Any ideas?

Thanks,

Alex
 
G

Greg Burns

I am not sure what he meant by not having an Item property either.

Our code does the same thing.

Request.Querystring(i) and Request.Querystring.Item(i) are equivalent. (default indexer and all that)

I think Bruce is probably correct about this not following w3c standards and probably should be avoided altogther.

Greg

How could you not have the QueryString.Item property??? It's the same object collection in VB.Net as it is in C#....anywho, what my routine does is looks for the VALUE named "client" and not the KEY. That is why QueryString(Key) gives you a value of Nothing. There is no KEY called "client", but there is a value called "client" that is not associated with a key (the key itself is "" or Nothing).

Try QueryString instead of QueryString.Item. Maybe that will work :)

Mythran

QueryString("client") does indeed return a value of Nothing (in VB), so that won't help you. But you can search to see if the key exists in the collection...

This should work. BTW, it is essential the same as Mythran posted previously.

Dim found As Boolean = False
For i As Integer = 0 To Request.QueryString.Count - 1
If Request.QueryString(i).ToLower = "client" Then
found = True
Exit For
End If
Next

Greg

Ok, just for this scenario, the only QueryString key on the url is client, as in www...com/main.aspx?client. QueryString["client"] will return null. QueryString.Keys.Count will return 1. I have no way of telling that the QS client is present b/c it does not have a value assigned to it. I could write a function to search parts of the url, but I wanted to see if any answers would surface first.

I do not have a QueryString.Item in C#. I do have other name value collections, but none of them work for me as I reported in the original post. Any more ideas?

Thanks

Alex
Private Function KeyInQuery(ByVal Key As String) As Boolean
Dim keyName As String
Dim val As String

For i As Integer = 0 To Request.QueryString.Count - 1
val = Request.QueryString.Item(i)

If Not val Is Nothing AndAlso val.ToLower() = Key.ToLower()
Return True
End If
Next

Return False
End Function

Try the above function :)

Mythran
I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ...

www.something.com/main.aspx?client

Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1. QueryString["client"] is null, QueryString.Keys[0] is null, QueryString.Keys["client"] is null, Request.QueryStingText = "client", ect.

I cannot seem to find a way to determine if a QuerySting exists when it has no value assigned to it. Any ideas?

Thanks,

Alex
 
L

Larry Viezel

I have a similar issue two weeks after you did. I use a bit of code
that loops through a querystring, removes the keyvalue pair I no
longer want in there and then redirects the user to the same page with
the newly formed querystring. Trouble is that the code would choke
when it hit a GetKey value that was null. And the reason it was null?
Because there was no = sign after it. We would only find users getting
this error when coming in from email clients that were cutting off the
URL with the correct querystring. So its no big deal but it was
annoying.

Lets use your example of "count". Logically programmers expect "count"
to be a key. People suggest that the proper w3c compliant way to fix
your problem would be "count=1" or "count=true" or heck even "count=".
Truth is though you are not using count as a key (even though most
programmer expect it to be one) we are using it just like we'd use a
value.

So whos to blame here? .Net for handling this instance as a value and
giving us a null GetKey result? Or developers for thinking its a key
but using it as a value? It seems a bit perplexing and the programming
purist in me wants to punch someone for it I just don't know who.

The solution in my case was to explicitly check for a null GetKey
value and then handle it accordingly. I programmed it in but I did so
reluctantly and under protest, cursing .net while doing so.

Larry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top