Querystring Questions

CButcher

New member
Joined
Feb 5, 2008
Messages
3
Programming Experience
10+
I've been working in C# building WinForms for almost 7 years and now I find myself needing to build a website in VB. I am having a heck of a time with a querystring problem, but I bet it will be old news to most of you.

If the querystring comes to me encoded, Request.QueryString fails.

Using the code:
myVar = CInt(Request.QueryString("Var2"))

With the querystring:
myPage.aspx?var1=20&var2=20
Everything works fine.

With the querystring:
myPage.aspx?var1='test'&var2='test' (Which is apparently how it is encoded when the user comes through a search engine.)
The code bombs with the error "Conversion from string to type Integer is not valid." This happens because Request.QueryString does not contain a value for Var2. It's actually now named "amp;Var2".

I figured this would be as easy as calling some type of HTMLDecode of off the request object, but that doesn't exist. And the one that exists in System.Web.HttpUtility only accepts a string as input.

Aside from the fact that tryparse would be better (blah, blah blah) what rookie mistake am I making?
 
With the querystring:
myPage.aspx?var1='test'&var2='test' (Which is apparently how it is encoded when the user comes through a search engine.)
The code bombs with the error "Conversion from string to type Integer is not valid." This happens because Request.QueryString does not contain a value for Var2. It's actually now named "amp;Var2"
?? Your posted querystring does contain a value and that value is 'test'. If the querystring object did not contain a value, you would receive an error stating something to that effect.
The Request object handles the HTML decoding internally and can read the &var2 as &var2 since & decoded is &.
Note that 'test' is a string and trying to cast it to an integer using CInt will give you the error you showed.

The answer to your problem, as you state, is using Integer.TryParse.
 
I appreciate the response, however, the problem is not that an error is thrown when trying to parse the INT. That part isn't really even needed to express this issue and I should have left it out.

The problem is that when the URL is HTML encoded the Request object is NOT decoding the querystring. When the Request.QueryString ValueCollection is checked at runtime it does not contain the key "var2" but it does contain the key "amp;Var2". When I access the variable "Request.QueryString("Var2")" the Request object returns an empty string, but does not throw an exception. Hence the "Conversion from string to type Integer is not valid" exception. Which is the expected behaviour.


The querystring example should have been:
myPage.aspx?var1=1&var2=2


?? Your posted querystring does contain a value and that value is 'test'. If the querystring object did not contain a value, you would receive an error stating something to that effect.
The Request object handles the HTML decoding internally and can read the &var2 as &var2 since & decoded is &.
Note that 'test' is a string and trying to cast it to an integer using CInt will give you the error you showed.

The answer to your problem, as you state, is using Integer.TryParse.
 
So what you're saying is that someone mangled a link and you get an invalid request for your page?
 
Is that what it is? I was told that this was how the requests came in from the search engine hits, but I'm begining to suspect that they gave me some bad info. From other things I read, it sounded like a URL should never come to me HTML encoded, but rather URL encoded. I just wanted to hear it from a different source. It's ok for the VALUES to be HTML encoded from what I read.

If that's the case, it's easy enough to wrap some checks around it.
 
myPage.aspx?var1='test'&var2='test'

If I was doing this, I would try to see if the request can be formatted properly. If it's not an option then I would use the Request.ServerVariables("Query_String") method and parse out the parameters.

Question: It seems like you are expecting an integer value, how come var2 is passing in a string value?
 
Back
Top