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

K

keithb

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
 
O

Onwuka Emeka

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

MSDN

Onwuka,

Please explain "on the safe side".

Thank you,

SA

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

keithb said:
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?=

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top