Query String Problem\VB

G

George

I am trying to check whether a query string is being passed to my .aspx page, like so:

If Not Request.QueryString Is Nothing Then
If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
Response.Redirect("../somepage.htm")
End If
Else
Response.Redirect("../somepage.htm")
End If

If there is a query string, everything is fine, but when there isn't one, the app crashes on the
second line :

If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then

:with this error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an
object.

What I don't understand, is why is it getting passed this line anyway:

If Not Request.QueryString Is Nothing Then

:when there wasn't even a query string passed? Why didn't it jump down to the Else part, instead?

Thanks,
George
 
C

Cezary Nolewajka

Hi George,

The point is that the QueryString property as such is always set and never
null (nothing). It is an object of NameValueCollection type that can have
zero elements (if query string is empty) but still is instantiated.

So, instead of checking the Request.QueryString existance, you should check
if it has the elemenent (field key) you are looking for:

string[] arString;
arString = Request.QueryString.GetValues("values");

if (arString != null)
{
...
}

Sorry for the c# code, but in nicely transposes to your VB.
 
G

George

Anatoly,

Apparently, ToString is not available in that context. Thanks for the suggestion, though.

George
 
G

George

Cezary,

I tried what you suggested and that solved my problem! Learn something new everyday.

Thanks for your help.
George


Cezary Nolewajka said:
Hi George,

The point is that the QueryString property as such is always set and never
null (nothing). It is an object of NameValueCollection type that can have
zero elements (if query string is empty) but still is instantiated.

So, instead of checking the Request.QueryString existance, you should check
if it has the elemenent (field key) you are looking for:

string[] arString;
arString = Request.QueryString.GetValues("values");

if (arString != null)
{
...
}

Sorry for the c# code, but in nicely transposes to your VB.

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply


George said:
I am trying to check whether a query string is being passed to my .aspx page, like so:

If Not Request.QueryString Is Nothing Then
If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
Response.Redirect("../somepage.htm")
End If
Else
Response.Redirect("../somepage.htm")
End If

If there is a query string, everything is fine, but when there isn't one, the app crashes on the
second line :

If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then

:with this error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an
object.

What I don't understand, is why is it getting passed this line anyway:

If Not Request.QueryString Is Nothing Then

:when there wasn't even a query string passed? Why didn't it jump down to the Else part, instead?

Thanks,
George
 

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

Top