Exception of type 'System.InvalidCastException' in generated Entity

Posts   
 
    
jvdboom
User
Posts: 28
Joined: 22-May-2007
# Posted on: 03-Mar-2008 11:37:08   

Hello,

I have a problem when i want to use an Entity: For example if i use: GenericDocumentEntity genDoc = new GenericDocumentEntity();

And then use the QuickWatch there is an error in the generated entity. 'genDoc.Appkey10' threw an exception of type 'System.InvalidCastException' while i didn't use the object.

Using LLBLGen 2.5 SQL server 2000 Script: CREATE TABLE [dbo].[IDB_LOG]( [Id] [int] IDENTITY(1,1) NOT NULL, [Business] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Company] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Refrencekey] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Appkey1] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Appkey2] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey3] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey4] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey5] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey6] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey7] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey8] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey9] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Appkey10] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Eventtime] [datetime] NOT NULL, [Channel] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Handling] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Target] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Batch] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Sortkey] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Groupkey] char COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Toshiptime] [datetime] NOT NULL, [OriginalFilename] char COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Recieved] [datetime] NOT NULL, [Archived] [datetime] NULL, [Stored] [datetime] NULL, [Inbatch] [datetime] NULL, [Documentgenerated] [datetime] NULL, [Sent] [datetime] NULL, [Pages] [numeric](18, 0) NOT NULL, [Setid] [numeric](18, 0) NULL, CONSTRAINT [PK_IDB_LOG] PRIMARY KEY CLUSTERED ( [Id] ASC ) ON [PRIMARY] ) ON [PRIMARY]

Template: Selfserving.

I hope you see the problem. This is the first time i have an error in the generated code.

Thanx already,

Jos

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-Mar-2008 12:09:14   

What's the CurrentValue value of the field of Appkey10 in the entity's Fields object?

Frans Bouma | Lead developer LLBLGen Pro
jvdboom
User
Posts: 28
Joined: 22-May-2007
# Posted on: 03-Mar-2008 13:10:20   

Hello Otis,

Thanx for the quick reply.

The value is nothing yet.

I only use:

DocEntity doc = new DocEntity();

Then in debug mode to the QuickWatch window. The result you see in the document i already attached.

In the database design it is a char(30) field.

Greetings, Jos

jvdboom
User
Posts: 28
Joined: 22-May-2007
# Posted on: 03-Mar-2008 13:24:51   

Otis,

I rerun the sql script and cleaned up everything. Things seems to work okay now.

Thanx for your time.

The problem is with thedata type which are not clear for LLBLGen but i can not find how the problem arises.

Greetiings, Jos

samanali
User
Posts: 2
Joined: 26-Oct-2010
# Posted on: 08-Mar-2012 11:36:27   

Hello dear, I have also such a problem 'System.InvalidCastException':

This happens when we add a new column to an existing table with a non nullable value and a default value

So I have added a column NumberOfAnswersDesired of type long (Number(1sunglasses ) and default value 0 to my table: Message_Type

I have also run a script on the database to assign value 0 for this column to all existing records.

In the LLBLGen project seems every thing ok, but when I call the value of the column I get always 'System.InvalidCastException'

 data.NumberOfAnswersDesired = entity.NumberOfAnswersDesired;

Here is the method which uses the retrived entity: public static MessageTypeData Entity2Data(MessageTypeEntity entity) { if (entity == null) { Exception exp = new ArgumentNullException("entity"); _log.Error(SRCommon.ArgumentNull("entity"), exp); throw exp; }

MessageTypeData data = new MessageTypeData(); try { data.Id = entity.Id; data.Name = entity.Name; data.NumberOfAnswersDesired = entity.NumberOfAnswersDesired; (Throws Exception) }

This problem has been ofen happen by ons Also for boelean data types.

Please help me to solve this. Best regards, Saman Ali 08-03-2012

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Mar-2012 22:42:25   

Hi Saman,

Please open a new thread, as this is very old and we prefer not to resurrect old ones. In your new thread please post the following: - LLBLGen version and runtime library version (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7725) - Exact error message and full stack trace - What is the type of data.NumberOfAnswersDesired and entity.NumberOfAnswersDesired? - What is the CurrentValue of entity.NumberOfAnswersDesired when the exception is thrown?

David Elizondo | LLBLGen Support Team