Input string was not in a correct format. on string "0"

Discussion in 'ASP .Net' started by chongo, Jul 7, 2004.

  1. chongo

    chongo Guest

    Hi,

    i'm reading in a bigint value from SQL Server.

    string s = myReader["BackColour"].ToString());

    int intBackColour = System.Convert.ToInt32(s);

    I'm also reading in other values the same way and it works great
    except this one value, i'm getting
    System.FormatException: Input string was not in a correct format.

    I don't understand because when I print out the string s to the screen
    it shows 0.

    Does anyone have a idea why its doing this?

    Any help would be greatly appreciated.
    -dp
     
    chongo, Jul 7, 2004
    #1
    1. Advertising

  2. chongo

    Guest

    On 7 Jul 2004 14:17:21 -0700, (chongo) wrote:

    >Hi,
    >
    >i'm reading in a bigint value from SQL Server.
    >
    >string s = myReader["BackColour"].ToString());
    >
    >int intBackColour = System.Convert.ToInt32(s);
    >
    >I'm also reading in other values the same way and it works great
    >except this one value, i'm getting
    >System.FormatException: Input string was not in a correct format.
    >
    >I don't understand because when I print out the string s to the screen
    >it shows 0.
    >
    >Does anyone have a idea why its doing this?
    >
    >Any help would be greatly appreciated.
    >-dp


    Very strange.

    Try

    int intBackColour = int.Parse(s);

    instead of

    int intBackColour = System.Convert.ToInt32(s);

    -Adam
     
    , Jul 7, 2004
    #2
    1. Advertising

  3. chongo

    Edward Guest

    Yes, remember to use Convert.ToInt32 carefully, sometimes it doesn't work to
    String convert , while Int32.Parse is ok.

    I guess, if two types are convertable, such as Int to BigInt, or Object
    to Int, System.Convert.Toxxx can work. String to Int is not
    convertable, so Convert.Toxxx may be wrong sometime.

    you can check MSDN for the types relation. That will give you details.

    > On 7 Jul 2004 14:17:21 -0700, (chongo) wrote:
    >
    > >Hi,
    > >
    > >i'm reading in a bigint value from SQL Server.
    > >
    > >string s = myReader["BackColour"].ToString());
    > >
    > >int intBackColour = System.Convert.ToInt32(s);
    > >
    > >I'm also reading in other values the same way and it works great
    > >except this one value, i'm getting
    > >System.FormatException: Input string was not in a correct format.
    > >
    > >I don't understand because when I print out the string s to the screen
    > >it shows 0.
    > >
    > >Does anyone have a idea why its doing this?
    > >
    > >Any help would be greatly appreciated.
    > >-dp

    >
    > Very strange.
    >
    > Try
    >
    > int intBackColour = int.Parse(s);
    >
    > instead of
    >
    > int intBackColour = System.Convert.ToInt32(s);
    >
    > -Adam
     
    Edward, Jul 8, 2004
    #3
    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. phmyhn
    Replies:
    0
    Views:
    11,442
    phmyhn
    Jun 24, 2003
  2. TJS
    Replies:
    0
    Views:
    1,364
  3. sbox
    Replies:
    2
    Views:
    15,374
    Ben Miller [MSFT]
    Feb 15, 2004
  4. Replies:
    0
    Views:
    631
  5. Maria
    Replies:
    4
    Views:
    8,507
    Maria
    Apr 4, 2004
Loading...

Share This Page