469,909 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

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

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
May 3 '06 #1
3 10016
To be on the safe side you can change to :
if(Com != Convert.ToString(rw.ItemArray[0]))

"keithb" <kb******@dslextreme.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

May 3 '06 #2
Onwuka,

Please explain "on the safe side".

Thank you,

SA

"Onwuka Emeka" <bu******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
To be on the safe side you can change to :
if(Com != Convert.ToString(rw.ItemArray[0]))

"keithb" <kb******@dslextreme.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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


May 3 '06 #3
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

May 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pankaj Jain | last post: by
reply views Thread by Fidias Gil de Montes | last post: by
1 post views Thread by Hifni Shahzard | last post: by
9 posts views Thread by Jim in Arizona | last post: by
1 post views Thread by =?Utf-8?B?U2NvdHQ=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.