Invalid cast from DBNull to Decimal

T

TheNortonZ

I must be just looking at this for so long I am going blind. Here is part of the code:

For Each _lRow In _laRow

Me.DepYrStraightLine.DepreciationYearID = CType((_lRow.Item("DepreciationYearID")), System.Guid)
Me.DepYrStraightLine.DepreciationID = CType((_lRow.Item("DepreciationID")), System.Guid)
Me.DepYrStraightLine.DepreciationTypeID = CType((_lRow.Item("DepreciationTypeID")), Integer)
Me.DepYrStraightLine.Year1 = CType((_lRow.Item("Year1")), Decimal)
Me.DepYrStraightLine.Year1BookValue = CType((_lRow.Item("Year1BookValue")), Decimal)
Me.DepYrStraightLine.Year2 = CType((_lRow.Item("Year2")), Decimal)
Me.DepYrStraightLine.Year2BookValue = CType((_lRow.Item("Year2BookValue")), Decimal)

The rows (or columns rather) Year1BookValue and Year2BookValue are two new columns I added to the table. I have gone all the way from the stored procs and tables and through the code to make sure everything was identical on the column data type settings. The data type is just like 'Year1' and 'Year2'. Decimal, default value of (0.0) 27 precision and 15 scale.

When I step through with the debugger and get to any of the new columns, I get an invalid cast exception that says the cast to DBNull from Decimal is not allowed. However, the 'Year1'/'Year2 fields have 0D in them.

Any idea what could be causing this problem?

Thanks.

Norton
 
D

Dave Fancher

DBNull and Decimal are incompatible data types. DBNull.Value represents the
null value stored in the database. Since the column has an undefined value,
it cannot be converted to any other type. You can resolve this issue by
checking if your column value equals DBNull.Value and assigning a default
value when it does otherwise assign the value from the column. If you're
using SQL Server, you could also rewrite the query to use the ISNULL
function (other DBMSs typically have a similar function).

This has been discussed/addressed in more detail before so you should search
for some of the previous posts...

-----

I must be just looking at this for so long I am going blind. Here is part of
the code:

For Each _lRow In _laRow
Me.DepYrStraightLine.DepreciationYearID =
CType((_lRow.Item("DepreciationYearID")), System.Guid)
Me.DepYrStraightLine.DepreciationID = CType((_lRow.Item("DepreciationID")),
System.Guid)
Me.DepYrStraightLine.DepreciationTypeID =
CType((_lRow.Item("DepreciationTypeID")), Integer)
Me.DepYrStraightLine.Year1 = CType((_lRow.Item("Year1")), Decimal)
Me.DepYrStraightLine.Year1BookValue = CType((_lRow.Item("Year1BookValue")),
Decimal)
Me.DepYrStraightLine.Year2 = CType((_lRow.Item("Year2")), Decimal)
Me.DepYrStraightLine.Year2BookValue = CType((_lRow.Item("Year2BookValue")),
Decimal)
The rows (or columns rather) Year1BookValue and Year2BookValue are two new
columns I added to the table. I have gone all the way from the stored procs
and tables and through the code to make sure everything was identical on the
column data type settings. The data type is just like 'Year1' and 'Year2'.
Decimal, default value of (0.0) 27 precision and 15 scale.
When I step through with the debugger and get to any of the new columns, I
get an invalid cast exception that says the cast to DBNull from Decimal is
not allowed. However, the 'Year1'/'Year2 fields have 0D in them.
Any idea what could be causing this problem?
Thanks.
Norton
 
T

TheNortonZ

Dave,

Thanks for answering.

What I actually found in my code was a location where I had first added a
row to the table. But, in the code where I add the row, I did not put any
default value in for the columns that were throwing exceptions, therefore,
they were null.

Thanks again.

Norton
 

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

Top