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?

 
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-03-2008
re:
!> I'm sorry, but you are in fact wrong

Explain to me what happens if the product id is w2344...




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)...
>> 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
 
 
 
 
jacerhea
Guest
Posts: n/a
 
      11-03-2008
On Nov 3, 10:51*am, "Juan T. Llibre" <(E-Mail Removed)>
wrote:
> re:
> !> I'm sorry, but you are in fact wrong
>
> Explain to me what happens if the product id is w2344...



You're arguing a completely different point now than what I said you
were wrong about, but ok. If you look at the original post, the
question contained the code...

"Dim intProductID As Integer = Trim(Request("productid")) "

Based on the original poster's own code, it is going to be an int. If
it is not going to be an int they should use the other piece of code
that I originally provided.


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
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-03-2008
re:
!> were wrong about, but ok. If you look at the original post, the
!> question contained the code...

!> "Dim intProductID As Integer = Trim(Request("productid")) "

See my explanantion of what a simple Request may return.
It may not necessarily return the value of the querystring.

What you both have proposed amounts to sloppy coding.
No offense intended.




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)...
On Nov 3, 10:51 am, "Juan T. Llibre" <(E-Mail Removed)>
wrote:
> re:
> !> I'm sorry, but you are in fact wrong
>
> Explain to me what happens if the product id is w2344...



You're arguing a completely different point now than what I said you
were wrong about, but ok. If you look at the original post, the
question contained the code...

"Dim intProductID As Integer = Trim(Request("productid")) "

Based on the original poster's own code, it is going to be an int. If
it is not going to be an int they should use the other piece of code
that I originally provided.


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
> See my explanantion of what a simple Request may return.
> It may not necessarily return the value of the querystring.


I never said otherwise. I provided two solutions, one merely adjusted
the logic the OP had provided, which did have the
Request["productid']. This was not my code, and I was merely
providing some guidance to prevent the problem they posted about.

I than provided my own solution which did use
Request.QueryString("productid").

 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      11-04-2008
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then

Dim o as Object = Request("productid")

If Not (o is Nothing) Then

Dim intProductID As Integer = Trim(o.ToString())

If IsNumeric(intProductID) Then
drpProducts.SelectedValue = intProductID
End If

Else
'What do you do when this happens
End If



End If
End Sub

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"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
 
George
Guest
Posts: n/a
 
      11-04-2008
Are we back to this????

What if the sun will not come up tomorrow.....
Any programming is based on assumptions.... And it's good to have those
assumptions.

Let say ProductId is id in MsSql database which is marked as integer. Then
the page will return an error trying to convert productid to integer and
database will be protected from virus that hits your site with urls like
this

/product.aspx?id=31147'%20and%20char(124)%2Buser%2B char(124)=0%20and%20''='


Again, let me repeat myself that any programming is based on assumptions.
Assumptions can be as simple as for example in DB first name is only limited
to 250 characters. It's an assumption. Very realistic but still it is.
George.


"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>
>> Dim o as Object = Request("productid")
>> If Not (o is Nothing) Then
>> Dim intProductID As Integer = Trim(o.ToString())

>
> And what if Request("productid") is "ABCD" ?
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


 
Reply With Quote
 
George
Guest
Posts: n/a
 
      11-04-2008
I do not think you need this IF
If IsNumeric(intProductID) Then

Since you already did
Dim intProductID As Integer = Trim(o.ToString())

Not sure if VB will eats this line like this though... I know C# compiler
will throw an error.

I would do something like this
Dim intProductID As Integer = Int32.Parse(Trim(o.ToString()))


George.


"Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> If Not Page.IsPostBack Then
>
> Dim o as Object = Request("productid")
>
> If Not (o is Nothing) Then
>
> Dim intProductID As Integer = Trim(o.ToString())
>
> If IsNumeric(intProductID) Then
> drpProducts.SelectedValue = intProductID
> End If
>
> Else
> 'What do you do when this happens
> End If
>
>
>
> End If
> End Sub
>
> --
> Gregory A. Beamer
> MVP, MCP: +I, SE, SD, DBA
>
> Subscribe to my blog
> http://feeds.feedburner.com/GregoryBeamer#
>
> or just read it:
> http://feeds.feedburner.com/GregoryBeamer
>
> ********************************************
> | Think outside the box! |
> ********************************************
> "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
 
George
Guest
Posts: n/a
 
      11-04-2008
"exceptions are handled" and "exceptions are not happening" 2 different
things...

I did not say that user needs to get ASP.NET error screen with bunch of
information if something is not integer.
But if user decided to play with QueryString by hand and put into ProductId
his name instead of number he deserves to be redirected to the default.aspx
how I do it in case of exception that is caught in Application_OnError

So it's perfectly OK to assume that ProductId is a number (if it's in
database) and do a conversion without tipping-toeing around.


George.





"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "George" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Are we back to this????
>>
>> What if the sun will not come up tomorrow.....
>> Any programming is based on assumptions.... And it's good to have those
>> assumptions.

>
> Utter rubbish! Utter, utter garbage!
>
> Robust programming (look it up on Google) is based on doing whatever you
> can to make sure that exceptions are handled.
>
> Trying to instantiate an integer from something which cannot be converted
> into an integer will cause an exception.
>
> Trapping for that eventualilty will not...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-04-2008
George,

I think the point is that we can't assume a general rule for product id's.

Even if in this case it were to be numeric,
programmers simply can't assume that to be the general case.

Why base your programming logic on an assumption which might not be true ?
Why not write code which will work regardless of whether the product id is a number or a 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:(E-Mail Removed)...
> Are we back to this????
>
> What if the sun will not come up tomorrow.....
> Any programming is based on assumptions.... And it's good to have those assumptions.
>
> Let say ProductId is id in MsSql database which is marked as integer. Then the page will return an error trying to
> convert productid to integer and database will be protected from virus that hits your site with urls like this
>
> /product.aspx?id=31147'%20and%20char(124)%2Buser%2B char(124)=0%20and%20''='
>
>
> Again, let me repeat myself that any programming is based on assumptions.
> Assumptions can be as simple as for example in DB first name is only limited to 250 characters. It's an assumption.
> Very realistic but still it is.
> George.
>
>
> "Mark Rae [MVP]" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> "Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>> Dim o as Object = Request("productid")
>>> If Not (o is Nothing) Then
>>> Dim intProductID As Integer = Trim(o.ToString())

>>
>> And what if Request("productid") is "ABCD" ?
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net

>



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      11-04-2008
re:
!> So it's perfectly OK to assume that ProductId is a number (if it's in database)

I'd hate to be in your shoes when the time comes you have to eat those words. <g>




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)...
> "exceptions are handled" and "exceptions are not happening" 2 different things...
>
> I did not say that user needs to get ASP.NET error screen with bunch of information if something is not integer.
> But if user decided to play with QueryString by hand and put into ProductId his name instead of number he deserves to
> be redirected to the default.aspx how I do it in case of exception that is caught in Application_OnError
>
> So it's perfectly OK to assume that ProductId is a number (if it's in database) and do a conversion without
> tipping-toeing around.
>
>
> George.
>
>
>
>
>
> "Mark Rae [MVP]" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> "George" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>>
>>> Are we back to this????
>>>
>>> What if the sun will not come up tomorrow.....
>>> Any programming is based on assumptions.... And it's good to have those assumptions.

>>
>> Utter rubbish! Utter, utter garbage!
>>
>> Robust programming (look it up on Google) is based on doing whatever you can to make sure that exceptions are
>> handled.
>>
>> Trying to instantiate an integer from something which cannot be converted into an integer will cause an exception.
>>
>> Trapping for that eventualilty will not...
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net

>



 
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