"Specified cast is not valid" when accessing Int16 field

Posts   
 
    
virginia
User
Posts: 27
Joined: 29-Nov-2016
# Posted on: 20-Mar-2018 10:59:07   

Hello.

I'm using LLBL 5.3 (5.3.0) RTM and .net 4.5.2, connecting to sql server 2005 and ora11g databases, db first, adapter model.

I have a problem with a field that's number(4) in the database. In the Oracle version, the LLBL designer sees the field directly as short. In the Sql Server version, it's seen as decimal, so I used the SD.LLBLGen.Pro.TypeConverters.Int16DecimalConverter on it.

The generated code (c#,platform .NET 4.5.2, SD.Presetds.Adapter.General) is

/// <summary> The SREF_MAINTENANCE property of the Entity STOCK<br/><br/></summary>
        /// <remarks>Mapped on  table field: "STOCK"."SREF_MAINTENANCE"<br/>
        /// Table field type characteristics (type, precision, scale, length): Decimal, 4, 0, 0<br/>
        /// Table field behavior characteristics (is nullable, is PK, is identity): false, false, false</remarks>
        public virtual System.Int16 SREF_MAINTENANCE
        {
            get { return (System.Int16)GetValue((int)STOCKFieldIndex.SREF_MAINTENANCE, true); }
            set { SetValue((int)STOCKFieldIndex.SREF_MAINTENANCE, value); }
        }

After fetching an entity, when trying to get the value, '((ASTech.LLBL.EntityClasses.STOCKEntity)entityToSave).SREF_MAINTENANCE' threw an exception of type 'System.InvalidCastException' " at ASTech.LLBL.EntityClasses.STOCKEntity.get_SREF_MAINTENANCE() in R:\\astech.llbl\\trunk\\EntityClasses\\STOCKEntity.cs:line 3247"

GetValue((int)STOCKFieldIndex.SREF_MAINTENANCE, true) return 0, which obviously cannot be converted to short smile Am I doing something wrong?

I see the generated code for the ooooooold version (v1) we've been using until recently looks completely different public virtual System.Int16 SREF_MAINTENANCE { get { object valueToReturn = base.GetCurrentFieldValue((int)STOCKFieldIndex.SREF_MAINTENANCE); if(valueToReturn == null) { valueToReturn = TypeDefaultValue.GetDefaultValue(typeof(System.Int16)); } return Convert.ToInt16(valueToReturn); } set { if(SetNewFieldValue((int)STOCKFieldIndex.SREF_MAINTENANCE, value)) { OnSREF_MAINTENANCEChanged(); } } }

Should I make a change in the templates or something? Thanks

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 20-Mar-2018 17:13:35   

Does it succeed if the value is not Zero?

virginia
User
Posts: 27
Joined: 29-Nov-2016
# Posted on: 21-Mar-2018 09:23:48   

No, no luck with 1 or other values

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 21-Mar-2018 11:40:51   

The value is stored as a decimal despite the type converter it seems. You're sure you've set the typeconverter on the sqlserver mapping (Likely otherwise you'd get an error but still), and when you fetch data from the oracle DB it's indeed working but fetching data from the SQL Server db it doesn't? If you set a breakpoint on the getter of the property, the value returned by GetValue((int)STOCKFieldIndex.SREF_MAINTENANCE, true), is that a decimal or a short?

Frans Bouma | Lead developer LLBLGen Pro
virginia
User
Posts: 27
Joined: 29-Nov-2016
# Posted on: 23-Mar-2018 09:30:24   

The type converter is set correctly, and yes for ora it works for sql server not. But I have to do some digging, it seems the entity is not fetched directly from the DB. There's a DataRow from which we fill the entity's fields in order to save it, so it may have something to do with that. I'll post here if I find anything.

virginia
User
Posts: 27
Joined: 29-Nov-2016
# Posted on: 23-Mar-2018 16:13:44   

Got it. Somewhere along the way there was a ForcedCurrentValueWrite call with decimals as parameters. Thanks for you patience.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Mar-2018 09:15:31   

Ah! good you found it simple_smile

Frans Bouma | Lead developer LLBLGen Pro