472,111 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

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 10138
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.