What if the Query String is empty?

Discussion in 'ASP .Net' started by MU, Nov 3, 2008.

  1. MU

    MU Guest

    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
    MU, Nov 3, 2008
    #1
    1. Advertising

  2. MU

    George Guest

    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" <> wrote in message
    news:...
    > 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, Nov 3, 2008
    #2
    1. Advertising

  3. MU

    jacerhea Guest

    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.FindByValue(productID)) >
    -1 Then
    drpProducts.SelectedValue = productID
    End If
    End If
    jacerhea, Nov 3, 2008
    #3
  4. MU

    jacerhea Guest


    > 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, Nov 3, 2008
    #4
  5. 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" <> wrote in message
    news:...
    >
    >> 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, Nov 3, 2008
    #5
  6. MU

    George Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >>
    >>> 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, Nov 3, 2008
    #6
  7. 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" <> wrote in message news:%232u$...
    > 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" <> wrote in message news:...
    >> 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" <> wrote in message
    >> news:...
    >>>
    >>>> 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, Nov 3, 2008
    #7
  8. MU

    George Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%232u$...
    >> 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" <> wrote in message
    >> news:...
    >>> 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" <> wrote in message
    >>> news:...
    >>>>
    >>>>> 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, Nov 3, 2008
    #8
  9. MU

    jacerhea Guest

    > 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....
    jacerhea, Nov 3, 2008
    #9
  10. 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" <> wrote in message news:...
    > 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" <> wrote in message news:...
    >> 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" <> wrote in message news:%232u$...
    >>> 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" <> wrote in message news:...
    >>>> 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" <> wrote in message
    >>>> news:...
    >>>>>
    >>>>>> 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, Nov 3, 2008
    #10
  11. 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" <> wrote in message
    news:...
    >> 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, Nov 3, 2008
    #11
  12. MU

    jacerhea Guest

    On Nov 3, 10:51 am, "Juan T. Llibre" <>
    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.FindByValue(productID)) >
    -1 Then
    drpProducts.SelectedValue = productID
    End If
    End If
    jacerhea, Nov 3, 2008
    #12
  13. 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" <> wrote in message
    news:...
    On Nov 3, 10:51 am, "Juan T. Llibre" <>
    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.FindByValue(productID)) >
    -1 Then
    drpProducts.SelectedValue = productID
    End If
    End If
    Juan T. Llibre, Nov 3, 2008
    #13
  14. MU

    jacerhea Guest

    > 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").
    jacerhea, Nov 3, 2008
    #14
  15. 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" <> wrote in message
    news:...
    > 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
    Cowboy \(Gregory A. Beamer\), Nov 4, 2008
    #15
  16. MU

    George Guest

    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%2Bchar(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]" <> wrote in message
    news:...
    > "Cowboy (Gregory A. Beamer)" <> wrote in
    > message news:...
    >
    >> 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
    George, Nov 4, 2008
    #16
  17. MU

    George Guest

    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)" <> wrote in
    message news:...
    > 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" <> wrote in message
    > news:...
    >> 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, Nov 4, 2008
    #17
  18. MU

    George Guest

    "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]" <> wrote in message
    news:...
    > "George" <> wrote in message
    > news:...
    >
    >> 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
    George, Nov 4, 2008
    #18
  19. 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" <> wrote in message news:...
    > 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%2Bchar(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]" <> wrote in message news:...
    >> "Cowboy (Gregory A. Beamer)" <> wrote in message
    >> news:...
    >>
    >>> 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

    >
    Juan T. Llibre, Nov 4, 2008
    #19
  20. 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" <> wrote in message news:...
    > "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]" <> wrote in message news:...
    >> "George" <> wrote in message news:...
    >>
    >>> 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

    >
    Juan T. Llibre, Nov 4, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul M.
    Replies:
    2
    Views:
    7,632
    Dave Bush
    Sep 6, 2003
  2. Tzury Bar Yochay
    Replies:
    1
    Views:
    402
    Gabriel Genellina
    Mar 24, 2008
  3. Savvoulidis Iordanis

    Empty gridview cells and checking for empty string

    Savvoulidis Iordanis, Sep 5, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    518
    Savvoulidis Iordanis
    Sep 5, 2008
  4. roadrunner
    Replies:
    1
    Views:
    234
    Gunnar Hjalmarsson
    Feb 8, 2006
  5. nick
    Replies:
    1
    Views:
    450
    David Mark
    Feb 13, 2011
Loading...

Share This Page