Help: Unable to cast object of type 'System.Int32' to type 'System.String'.

Discussion in 'ASP .Net' started by keithb, May 3, 2006.

  1. keithb

    keithb Guest

    What could be causing this?

    this code:

    String Com = "";
    if (Com != (String)rw.ItemArray[0])

    fails at runtime with the error message: Unable to cast object of type
    'System.Int32' to type 'System.String'.

    this code, in a different class runs without error:
    String Env = "";
    if (Env != (String)rw.ItemArray[0])

    Both rw.ItemArray[0] elements are int32 datatypes, both are populated with
    the same numerical value

    Thanks,

    Keith
     
    keithb, May 3, 2006
    #1
    1. Advertising

  2. keithb

    Onwuka Emeka Guest

    Re: Unable to cast object of type 'System.Int32' to type 'System.String'.

    To be on the safe side you can change to :
    if(Com != Convert.ToString(rw.ItemArray[0]))

    "keithb" <> wrote in message
    news:%...
    > What could be causing this?
    >
    > this code:
    >
    > String Com = "";
    > if (Com != (String)rw.ItemArray[0])
    >
    > fails at runtime with the error message: Unable to cast object of type
    > 'System.Int32' to type 'System.String'.
    >
    > this code, in a different class runs without error:
    > String Env = "";
    > if (Env != (String)rw.ItemArray[0])
    >
    > Both rw.ItemArray[0] elements are int32 datatypes, both are populated with
    > the same numerical value
    >
    > Thanks,
    >
    > Keith
    >
     
    Onwuka Emeka, May 3, 2006
    #2
    1. Advertising

  3. keithb

    MSDN Guest

    Re: Unable to cast object of type 'System.Int32' to type 'System.String'.

    Onwuka,

    Please explain "on the safe side".

    Thank you,

    SA

    "Onwuka Emeka" <> wrote in message
    news:%...
    > To be on the safe side you can change to :
    > if(Com != Convert.ToString(rw.ItemArray[0]))
    >
    > "keithb" <> wrote in message
    > news:%...
    >> What could be causing this?
    >>
    >> this code:
    >>
    >> String Com = "";
    >> if (Com != (String)rw.ItemArray[0])
    >>
    >> fails at runtime with the error message: Unable to cast object of type
    >> 'System.Int32' to type 'System.String'.
    >>
    >> this code, in a different class runs without error:
    >> String Env = "";
    >> if (Env != (String)rw.ItemArray[0])
    >>
    >> Both rw.ItemArray[0] elements are int32 datatypes, both are populated
    >> with the same numerical value
    >>
    >> Thanks,
    >>
    >> Keith
    >>

    >
    >
     
    MSDN, May 3, 2006
    #3
  4. You can never cast an int to a string. You can only cast an object to
    it's actual type or any type that it inherits.

    Use the ToString method to convert an int to a string.

    Your code doesn't make sense, though. The string representation of an
    int could never be equal to an empty string.

    keithb wrote:
    > What could be causing this?
    >
    > this code:
    >
    > String Com = "";
    > if (Com != (String)rw.ItemArray[0])
    >
    > fails at runtime with the error message: Unable to cast object of type
    > 'System.Int32' to type 'System.String'.
    >
    > this code, in a different class runs without error:
    > String Env = "";
    > if (Env != (String)rw.ItemArray[0])
    >
    > Both rw.ItemArray[0] elements are int32 datatypes, both are populated with
    > the same numerical value
    >
    > Thanks,
    >
    > Keith
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 7, 2006
    #4
    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. =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=

    Invalid cast from System.Int32 to System.Byte[].

    =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=, Feb 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,826
    William F. Robertson, Jr.
    Feb 15, 2005
  2. Imran Aziz
    Replies:
    4
    Views:
    8,770
    ljenner01
    Jan 19, 2011
  3. Replies:
    0
    Views:
    903
  4. Chris
    Replies:
    1
    Views:
    1,014
    Teemu Keiski
    Feb 12, 2007
  5. Replies:
    2
    Views:
    1,630
Loading...

Share This Page