[FIXED] InvalidCastException with null data in a money column

Posts   
 
    
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 02-Oct-2003 21:18:46   

Whenever I have a null value in a money column I'm getting the following error:

System.InvalidCastException: Specified cast is not valid.

Line 830:                   throw new ORMFieldIsNullException("Cannot get the value for Price because it is set to NULL.");
Line 831:               }
Line 832:               return (System.Decimal)valueToReturn;
Line 833:           }
Line 834:           set


C:\Projects\Applications\IntranetPortal\DAL.Portal\EntityClasses\EquipmentEntity.cs Line: 832 

I'm guessing this is not intentional since I have null data in other columns and it's not crashing with any of those, except a null value in a DateTime field returns "1/1/0001". Any ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39881
Joined: 17-Aug-2003
# Posted on: 02-Oct-2003 21:52:53   

Money columns should be set to 0.0 when they're null. I'll look into it.

The values columns with a NULL value get is stored in the TypeDefaultValue.cs/vb class (helperclasses folder). For a money column this is a System.Decimal type, datetime columns will receive DateTime.MinValue.

(you can test for NULL values for a field by using entityobject.TestOriginalFieldValueForNull method btw)

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 02-Oct-2003 22:13:07   

Otis wrote:

Money columns should be set to 0.0 when they're null. I'll look into it.

The values columns with a NULL value get is stored in the TypeDefaultValue.cs/vb class (helperclasses folder). For a money column this is a System.Decimal type, datetime columns will receive DateTime.MinValue.

(you can test for NULL values for a field by using entityobject.TestOriginalFieldValueForNull method btw)

That datetime thing makes sense, I can test for DateTime.MinValue. The only problem I can see with that technique is that sql server doesn't like dates earlier than 1/1/1753, so a datetime.minval in .net will crash it (not that you should let that go unchecked). It's easy enough to check for though.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39881
Joined: 17-Aug-2003
# Posted on: 03-Oct-2003 11:02:47   

Cadmium wrote:

Otis wrote:

Money columns should be set to 0.0 when they're null. I'll look into it.

The values columns with a NULL value get is stored in the TypeDefaultValue.cs/vb class (helperclasses folder). For a money column this is a System.Decimal type, datetime columns will receive DateTime.MinValue.

(you can test for NULL values for a field by using entityobject.TestOriginalFieldValueForNull method btw)

That datetime thing makes sense, I can test for DateTime.MinValue. The only problem I can see with that technique is that sql server doesn't like dates earlier than 1/1/1753, so a datetime.minval in .net will crash it (not that you should let that go unchecked). It's easy enough to check for though.

A datetime.minvalue will never end up in the database, because only fields which have been changed will be updated, the datetime.minvalue is a value the field gets when it is read from the database, that doesn't count for a change. So when you read an entity from the database and it contains a null in a datetime column and you change another field's value and you call Save() only the field you changed is updated.

When you have a new entity, it will be prefilled with the default values. When you want a column to have a given value, for example NULL, you have to set it to that value explicitly.

I could reproduce your problem here. I'll check out how to fix it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39881
Joined: 17-Aug-2003
# Posted on: 03-Oct-2003 11:22:01   

It was caused by the fact that in the TypeDefaultValue.cs/vb class the value was set to 0.0. Compilers for C# and VB.NET think when they see '0.0' it's a double, so the value was in fact seen as a double.

I've now changed the templates so the value for the decimal type will be 0.0M, which works simple_smile . The templates will be released later today (October 3).

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39881
Joined: 17-Aug-2003
# Posted on: 03-Oct-2003 15:37:04   

Fixed in template sets released on 03-oct-2003. simple_smile

Frans Bouma | Lead developer LLBLGen Pro