Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   What if the Query String is empty? (http://www.velocityreviews.com/forums/t642746-what-if-the-query-string-is-empty.html)

MU 11-03-2008 03:35 AM

What if the Query String is empty?
 
Hello

I have some code that sets a dropdownlist control with a parameter
from the querystring. However, when the querystring is empty, I get
an error.

Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim intProductID As Integer = Trim(Request("productid"))
If IsNumeric(intProductID) Then
drpProducts.SelectedValue = intProductID
End If
End If
End Sub

Here is the error:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a
correct format.

Source Error:
Line 18: Protected Sub Page_Load(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Me.Load
Line 19: If Not Page.IsPostBack Then
Line 20: Dim intProductID As Integer =
Trim(Request("productid"))

How do I just have it continue without giving me the error?

Thanks

George 11-03-2008 03:53 AM

Re: What if the Query String is empty?
 
My guess this line
Dim intProductID As Integer = Trim(Request("productid"))
assumes that productid is Integer....and blows when it's not (empty)

As of "how do you continue without getting an error" I would say stop
assuming it's Integer and start assuming it's String
Dim intProductID As String = Trim(Request("productid"))


George.





"MU" <michael@lonelyprogrammer.com> wrote in message
news:4f476c12-e229-4972-a1df-ff06df87ab80@d10g2000pra.googlegroups.com...
> Hello
>
> I have some code that sets a dropdownlist control with a parameter
> from the querystring. However, when the querystring is empty, I get
> an error.
>
> Here is my code:
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> If Not Page.IsPostBack Then
> Dim intProductID As Integer = Trim(Request("productid"))
> If IsNumeric(intProductID) Then
> drpProducts.SelectedValue = intProductID
> End If
> End If
> End Sub
>
> Here is the error:
> Input string was not in a correct format.
> Description: An unhandled exception occurred during the execution of
> the current web request. Please review the stack trace for more
> information about the error and where it originated in the code.
>
> Exception Details: System.FormatException: Input string was not in a
> correct format.
>
> Source Error:
> Line 18: Protected Sub Page_Load(ByVal sender As Object, ByVal e
> As System.EventArgs) Handles Me.Load
> Line 19: If Not Page.IsPostBack Then
> Line 20: Dim intProductID As Integer =
> Trim(Request("productid"))
>
> How do I just have it continue without giving me the error?
>
> Thanks



jacerhea 11-03-2008 09:47 AM

Re: What if the Query String is empty?
 
You just need to rearrange your logic a bit...
If Not Page.IsPostBack Then
If IsNumeric(Trim(Request("productid"))) Then
drpProducts.SelectedValue =
Trim(Request.QueryString("productid"))
End If
End If

Or if you also want to make sure the value actually exists, try
this...

If Not Page.IsPostBack Then
Dim productID As String = Request.QueryString("productID")
If
drpProducts.Items.IndexOf(drpProducts.Items.FindBy Value(productID)) >
-1 Then
drpProducts.SelectedValue = productID
End If
End If

jacerhea 11-03-2008 01:38 PM

Re: What if the Query String is empty?
 

> And what if the QueryString is completely empty, as per the title of the
> thread...?


Than IsNumeric will not evaluate to true. I just tested it and it
works...

Juan T. Llibre 11-03-2008 02:21 PM

Re: What if the Query String is empty?
 
re:
!> Than IsNumeric will not evaluate to true

Querystrings always are Strings, and never Numeric.

If IsNumeric(Trim(Request("productid"))) will *always* evaluate to false.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"jacerhea" <jace.rhea@gmail.com> wrote in message
news:933bdb2e-c9a2-44ad-9f73-929b26bbd4fc@w1g2000prk.googlegroups.com...
>
>> And what if the QueryString is completely empty, as per the title of the
>> thread...?

>
> Than IsNumeric will not evaluate to true. I just tested it and it
> works...




George 11-03-2008 02:49 PM

Re: What if the Query String is empty?
 
wrong....

from msdn

IsNumeric returns True if the data type of Expression is Short, Integer,
Long, Decimal, Single, or Short. It also returns True if Expression is a
String that can be successfully converted to a Double. It returns False if
Expression is of data type Date."

George

"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:etYv79bPJHA.3936@TK2MSFTNGP06.phx.gbl...
> re:
> !> Than IsNumeric will not evaluate to true
>
> Querystrings always are Strings, and never Numeric.
>
> If IsNumeric(Trim(Request("productid"))) will *always* evaluate to false.
>
>
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaņol : http://asp.net.do/foros/
> ======================================
> "jacerhea" <jace.rhea@gmail.com> wrote in message
> news:933bdb2e-c9a2-44ad-9f73-929b26bbd4fc@w1g2000prk.googlegroups.com...
>>
>>> And what if the QueryString is completely empty, as per the title of the
>>> thread...?

>>
>> Than IsNumeric will not evaluate to true. I just tested it and it
>> works...

>
>



Juan T. Llibre 11-03-2008 03:03 PM

Re: What if the Query String is empty?
 
re:
!> wrong....

You should be a bit more careful when accusing somebody of being "wrong".

To repeat : a querystring is *always* a string.

A query string is a NameValueCollection containing
the collection of query string variables sent by the client.

The datatype for a *query string* can never be Numeric.
The very name itself is a hint for its data type : query *string*.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"George" <noemail@comcast.net> wrote in message news:%232u$VNcPJHA.2312@TK2MSFTNGP03.phx.gbl...
> wrong....
>
> from msdn
>
> IsNumeric returns True if the data type of Expression is Short, Integer, Long, Decimal, Single, or Short. It also
> returns True if Expression is a String that can be successfully converted to a Double. It returns False if Expression
> is of data type Date."
>
> George
>
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message news:etYv79bPJHA.3936@TK2MSFTNGP06.phx.gbl...
>> re:
>> !> Than IsNumeric will not evaluate to true
>>
>> Querystrings always are Strings, and never Numeric.
>>
>> If IsNumeric(Trim(Request("productid"))) will *always* evaluate to false.
>>
>>
>>
>>
>> Juan T. Llibre, asp.net MVP
>> asp.net faq : http://asp.net.do/faq/
>> foros de asp.net, en espaņol : http://asp.net.do/foros/
>> ======================================
>> "jacerhea" <jace.rhea@gmail.com> wrote in message
>> news:933bdb2e-c9a2-44ad-9f73-929b26bbd4fc@w1g2000prk.googlegroups.com...
>>>
>>>> And what if the QueryString is completely empty, as per the title of the
>>>> thread...?
>>>
>>> Than IsNumeric will not evaluate to true. I just tested it and it
>>> works...

>>
>>

>




George 11-03-2008 03:13 PM

Re: What if the Query String is empty?
 
ok , now you lost me....

I quoted MSDN where it says that IsNumeric(string) will return true if
string can be converted to Double.

You said that
IsNumeric(Trim(Request("productid")))
will always evaluate to false....

I pointed out that you wrong and it will evaluate to TRUE if
Request("productid") returns something like "123"

PS: accusing probably is a strong word.. I did not offer a jail term for
you. May be I should have said "mistaken"? Applogies then... I was in a
hurry...


George.



"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:uRXRKVcPJHA.4760@TK2MSFTNGP02.phx.gbl...
> re:
> !> wrong....
>
> You should be a bit more careful when accusing somebody of being "wrong".
>
> To repeat : a querystring is *always* a string.
>
> A query string is a NameValueCollection containing
> the collection of query string variables sent by the client.
>
> The datatype for a *query string* can never be Numeric.
> The very name itself is a hint for its data type : query *string*.
>
>
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaņol : http://asp.net.do/foros/
> ======================================
> "George" <noemail@comcast.net> wrote in message
> news:%232u$VNcPJHA.2312@TK2MSFTNGP03.phx.gbl...
>> wrong....
>>
>> from msdn
>>
>> IsNumeric returns True if the data type of Expression is Short, Integer,
>> Long, Decimal, Single, or Short. It also returns True if Expression is a
>> String that can be successfully converted to a Double. It returns False
>> if Expression is of data type Date."
>>
>> George
>>
>> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
>> news:etYv79bPJHA.3936@TK2MSFTNGP06.phx.gbl...
>>> re:
>>> !> Than IsNumeric will not evaluate to true
>>>
>>> Querystrings always are Strings, and never Numeric.
>>>
>>> If IsNumeric(Trim(Request("productid"))) will *always* evaluate to
>>> false.
>>>
>>>
>>>
>>>
>>> Juan T. Llibre, asp.net MVP
>>> asp.net faq : http://asp.net.do/faq/
>>> foros de asp.net, en espaņol : http://asp.net.do/foros/
>>> ======================================
>>> "jacerhea" <jace.rhea@gmail.com> wrote in message
>>> news:933bdb2e-c9a2-44ad-9f73-929b26bbd4fc@w1g2000prk.googlegroups.com...
>>>>
>>>>> And what if the QueryString is completely empty, as per the title of
>>>>> the
>>>>> thread...?
>>>>
>>>> Than IsNumeric will not evaluate to true. I just tested it and it
>>>> works...
>>>
>>>

>>

>
>



jacerhea 11-03-2008 03:20 PM

Re: What if the Query String is empty?
 
> You should be a bit more careful when accusing somebody of being "wrong".
>
> To repeat : a querystring is *always* a string.
>
> A query string is a NameValueCollection containing
> the collection of query string variables sent by the client.
>
> The datatype for a *query string* can never be Numeric.
> The very name itself is a hint for its data type : query *string*.



I'm sorry, but you are in fact wrong. The querystring is a string.
We are all in agreement on that. But you said that IsNumeric(string)
will always evaluate to false, which is incorrect. If the string can
be cast to a double such as "1", "345", "6.4", or some other variant
the expression will evaluate to true. George found the appropriate
documentation and correctly pointed this out. Open VS and try it
yourself....

Juan T. Llibre 11-03-2008 04:50 PM

Re: What if the Query String is empty?
 
re:
!> I quoted MSDN where it says that IsNumeric(string) will return true if
!> string can be converted to Double.
!> I pointed out that you wrong and it will evaluate to TRUE if
!> Request("productid") returns something like "123"

That's a really large *if*...

Sure, but not all product id's are simple numeric strings.
You might have a product id like : w2344.

That won't evaluate the way you think it should.

re:
!> Request("productid")

Request works in a funny way.

When you do a plain request, without identifying the exact type of request you want,
the ASP.NET engine cycles through all request types and will return the first one it finds.

If you ask for Request("productid"), and there's a cookie named "productid",
that's what will be returned instead of the value of the querystring named "productid".

If there's a form with a field named "productid", you may get the form field's value.

You can't trust what a simple "request" returns.

You should always specify Request.QueryString, if you want to go that way
....and you must make sure that the potential value returned can only be numeric.

re:
!> May be I should have said "mistaken"?

Maybe you should have... ;-)

re:
!> Apologies then... I was in a hurry...

No problem...



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"George" <noemail@comcast.net> wrote in message news:eaK0EbcPJHA.3936@TK2MSFTNGP06.phx.gbl...
> ok , now you lost me....
>
> I quoted MSDN where it says that IsNumeric(string) will return true if string can be converted to Double.
>
> You said that
> IsNumeric(Trim(Request("productid")))
> will always evaluate to false....
>
> I pointed out that you wrong and it will evaluate to TRUE if Request("productid") returns something like "123"
>
> PS: accusing probably is a strong word.. I did not offer a jail term for you. May be I should have said "mistaken"?
> Applogies then... I was in a hurry...
>
>
> George.
>
>
>
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message news:uRXRKVcPJHA.4760@TK2MSFTNGP02.phx.gbl...
>> re:
>> !> wrong....
>>
>> You should be a bit more careful when accusing somebody of being "wrong".
>>
>> To repeat : a querystring is *always* a string.
>>
>> A query string is a NameValueCollection containing
>> the collection of query string variables sent by the client.
>>
>> The datatype for a *query string* can never be Numeric.
>> The very name itself is a hint for its data type : query *string*.
>>
>>
>>
>>
>> Juan T. Llibre, asp.net MVP
>> asp.net faq : http://asp.net.do/faq/
>> foros de asp.net, en espaņol : http://asp.net.do/foros/
>> ======================================
>> "George" <noemail@comcast.net> wrote in message news:%232u$VNcPJHA.2312@TK2MSFTNGP03.phx.gbl...
>>> wrong....
>>>
>>> from msdn
>>>
>>> IsNumeric returns True if the data type of Expression is Short, Integer, Long, Decimal, Single, or Short. It also
>>> returns True if Expression is a String that can be successfully converted to a Double. It returns False if
>>> Expression is of data type Date."
>>>
>>> George
>>>
>>> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message news:etYv79bPJHA.3936@TK2MSFTNGP06.phx.gbl...
>>>> re:
>>>> !> Than IsNumeric will not evaluate to true
>>>>
>>>> Querystrings always are Strings, and never Numeric.
>>>>
>>>> If IsNumeric(Trim(Request("productid"))) will *always* evaluate to false.
>>>>
>>>>
>>>>
>>>>
>>>> Juan T. Llibre, asp.net MVP
>>>> asp.net faq : http://asp.net.do/faq/
>>>> foros de asp.net, en espaņol : http://asp.net.do/foros/
>>>> ======================================
>>>> "jacerhea" <jace.rhea@gmail.com> wrote in message
>>>> news:933bdb2e-c9a2-44ad-9f73-929b26bbd4fc@w1g2000prk.googlegroups.com...
>>>>>
>>>>>> And what if the QueryString is completely empty, as per the title of the
>>>>>> thread...?
>>>>>
>>>>> Than IsNumeric will not evaluate to true. I just tested it and it
>>>>> works...
>>>>
>>>>
>>>

>>
>>

>





All times are GMT. The time now is 11:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.