how to validate QueryString?

Discussion in 'ASP .Net' started by Jassim Rahma, Jul 23, 2007.

  1. Jassim Rahma

    Jassim Rahma Guest

    I have an ID which I pass from the QueryString["id"]. The id is integer only
    and I want before performing any action to validate if the Id is valid
    integet.
     
    Jassim Rahma, Jul 23, 2007
    #1
    1. Advertising

  2. On Jul 23, 1:33 pm, "Jassim Rahma" <> wrote:
    > I have an ID which I pass from the QueryString["id"]. The id is integer only
    > and I want before performing any action to validate if the Id is valid
    > integet.


    for example

    int id;
    try {
    id = Request.QueryString["id"];
    } catch {
    return; // id is wrong
    }
     
    Alexey Smirnov, Jul 23, 2007
    #2
    1. Advertising

  3. Jassim Rahma

    Aidy Guest

    int id = 0;
    if (int.TryParse(QueryString["id"], out id))
    {
    // id is ok
    }
    else
    {
    // it isn't
    }

    "Jassim Rahma" <> wrote in message
    news:...
    >I have an ID which I pass from the QueryString["id"]. The id is integer
    >only
    > and I want before performing any action to validate if the Id is valid
    > integet.
    >
    >
     
    Aidy, Jul 23, 2007
    #3
  4. Alexey,

    You should do some research on using Try/Catch to take care of programming
    logic. Doing so should be avoided at all cost. Using Try/Catch is very
    inneficient and can make applications perform very slowy.

    Aidy's example utilizing TryParse is an incredibly more efficient way to
    handle this.

    Regards,

    --
    S. Justin Gengo, MCP
    justin@aboutfortunate[-NoSpam-].com

    Free code library at:
    www.aboutfortunate.com




    "Alexey Smirnov" <> wrote in message
    news:...
    > On Jul 23, 1:33 pm, "Jassim Rahma" <> wrote:
    >> I have an ID which I pass from the QueryString["id"]. The id is integer
    >> only
    >> and I want before performing any action to validate if the Id is valid
    >> integet.

    >
    > for example
    >
    > int id;
    > try {
    > id = Request.QueryString["id"];
    > } catch {
    > return; // id is wrong
    > }
    >
     
    S. Justin Gengo, Jul 23, 2007
    #4
  5. S. Justin Gengo wrote:
    > Alexey,
    >
    > You should do some research on using Try/Catch to take care of
    > programming logic. Doing so should be avoided at all cost. Using
    > Try/Catch is very inneficient and can make applications perform very slowy.
    >
    > Aidy's example utilizing TryParse is an incredibly more efficient way to
    > handle this.


    In this case either way is valid. A non-integer value might very well be
    considered to be an exceptional situation. In that case performance is
    not an issue. It's true that exceptions should not be used in normal
    program flow, but if the validation fails, the normal program flow is
    out of the picture anyway.

    -

    The catch, however, should _not_ catch any exception, it should catch
    only FormatException and OverflowException. Don't catch exceptions that
    you don't know how to handle.

    Also the code handling the exception should _not_ simply do a return, it
    should actually handle the situation.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jul 23, 2007
    #5
    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. suzy
    Replies:
    3
    Views:
    3,099
  2. Axel Dahmen

    Pack QueryString

    Axel Dahmen, Jun 29, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    537
    Axel Dahmen
    Jun 29, 2003
  3. Natty Gur

    Re: Pack QueryString

    Natty Gur, Jun 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    499
    Natty Gur
    Jun 29, 2003
  4. Mehdi
    Replies:
    6
    Views:
    36,127
    sloan
    Apr 6, 2006
  5. Adeel Ahmad
    Replies:
    1
    Views:
    528
    Anthony Jones
    Mar 7, 2006
Loading...

Share This Page