field type convertion error

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

why doesn't this work?

this.AnnvrsryDay = (Int16)myDataRow["AnnvrsryDay"];

this.AnnvrsryDay is a Int16 in the class

myDataRow["AnnvrsryDay"] is an Int16 in my database, and at the time
the error is generated = 24

Error: InvalidCastException
Specified cast is not valid.


If I don't do the cast at all (Since it's an Int16 all the way through
I get a complie error because myDataRow["AnnvrsryDay"] is an object,
and my only option is ToString(), which then fails to compile with
cna't convert string to 'short'

What am I missing?
 
Dan said:
this.AnnvrsryDay = (Int16)myDataRow["AnnvrsryDay"];

this.AnnvrsryDay is a Int16 in the class

myDataRow["AnnvrsryDay"] is an Int16 in my database, and at the time
the error is generated = 24

Error: InvalidCastException
Specified cast is not valid.

You are probably wrong in assuming that myDataRow["AnnvrsryDay"] is an
Int16. Even if it is a 16-bit integer in the database, the database drivers
may be converting it to something else when reading the data. You can use
the debugger to find out the real type, or you can use
System.Diagnostics.Debug.Writeline(myDataRow["AnnvrsryDay"].GetType().FullName)
to dump it to the debug output. In all likelyhood you will find out that it
is not System.Int16.

You can use Convert.ToInt16(myDataRow["AnnvrsryDay"]), or
short.Parse(myDataRow["AnnvrsryDay"].ToString()). Note that all of these
methods (including the cast) will fail if the field is NULL in the database
(it will contain DBNull.Value in the DataRow).
 
Try
   this.AnnvrsryDay = Int16.Parse(myDataRow["AnnvsryDay:].ToString());




why doesn't this work?
this.AnnvrsryDay = (Int16)myDataRow["AnnvrsryDay"];
this.AnnvrsryDay is a Int16 in the class
myDataRow["AnnvrsryDay"] is an Int16 in my database, and at the time
the error is generated = 24
Error: InvalidCastException
Specified cast is not valid.
If I don't do the cast at all (Since it's an Int16 all the way through
I get a complie error because myDataRow["AnnvrsryDay"] is an object,
and my only option is ToString(), which then fails to compile with
cna't convert string to 'short'
What am I missing?- Hide quoted text -

- Show quoted text -

That worked. Thanks.
 
Try
   this.AnnvrsryDay = Int16.Parse(myDataRow["AnnvsryDay:].ToString());

A better way to do that is Convert.ToInt16() - it will avoid round-
trip via a string if it can (e.g. if the source object is Int32).
 
why doesn't this work?

 this.AnnvrsryDay = (Int16)myDataRow["AnnvrsryDay"];

 this.AnnvrsryDay is a Int16 in the class

myDataRow["AnnvrsryDay"] is an Int16 in my database

I'm not aware of type called "int16" in SQL dialects. What is the type
of the field in the table, exactly, and what database is this?
 
Hello Dan,
why doesn't this work?

this.AnnvrsryDay = (Int16)myDataRow["AnnvrsryDay"];

this.AnnvrsryDay is a Int16 in the class

myDataRow["AnnvrsryDay"] is an Int16 in my database, and at the time
the error is generated = 24

Error: InvalidCastException
Specified cast is not valid.
If I don't do the cast at all (Since it's an Int16 all the way through
I get a complie error because myDataRow["AnnvrsryDay"] is an object,
and my only option is ToString(), which then fails to compile with
cna't convert string to 'short'

What am I missing?

Judging from the can't convert error and the fact that it mentions a short,
instead of an int16, I guess that you'll have to cast it to (short) instead.

The database might think it's an int, your class might think it's an int,
but your database driver thinks it's a short.

The easiest way to solve these kinds of errors is to place a breakpoint on
this row and take a look at the type in the watch window. The watch window
is usually (always) right about the type at runtime.

Ohh and for those who suggest to do a roundtrip though .Parse(.ToString),
please don't . While this solves the compiler error, it is a) expensive,
b) unnessecairy, c) might introduce rounding errors (for fleating types).
Convert is a better solution, although a simple cast is by far the easiest
and the fastest if the value already is of the specified type.
 
Judging from the can't convert error and the fact that it mentions a short,
instead of an int16, I guess that you'll have to cast it to (short) instead.

C# type keyword "short" is an alias for System.Int16.
 
Back
Top