Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > What if the Query String is empty?

Reply
Thread Tools

What if the Query String is empty?

 
 
MU
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
 
 
 
George
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
jacerhea
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
jacerhea
Guest
Posts: n/a
 
      11-03-2008

> 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...
 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>> 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...



 
Reply With Quote
 
George
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>>> 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...

>
>


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message news:%232u$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>
>>>> 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...

>>
>>

>



 
Reply With Quote
 
George
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%232u$(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>
>>>>> 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...
>>>
>>>

>>

>
>


 
Reply With Quote
 
jacerhea
Guest
Posts: n/a
 
      11-03-2008
> 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....
 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-03-2008
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message news:%232u$(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>>>> 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" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>>
>>>>>> 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...
>>>>
>>>>
>>>

>>
>>

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert url with query string to url without query string nick Javascript 1 02-13-2011 11:20 PM
Date string problem in query string when posting back =?Utf-8?B?SnVsaWEgQg==?= ASP .Net 5 08-29-2007 02:08 PM
CGI - How to use upload_hook and query the query string ? roadrunner Perl Misc 1 02-08-2006 01:50 AM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM



Advertisments