Error inserting object with PK not incremented

Posts   
 
    
raduch
User
Posts: 17
Joined: 26-Jan-2007
# Posted on: 30-Oct-2007 10:15:47   

Hello,

I have the following scenario: - I have a table "Tyre" with the structure:

Bac varchar(31) PK Eancode decimal(13, 0) TyreTypeID int .....

  • I create a TyreEntity and set the Bac field to a unique value and save it
  • this exception appears, and in the insert query it doesn't set the Bac field

Any ideea why this happens?

LLBL 2.5 using self servicing

{"An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'Bac', table 'TymaconOnline.dbo.Tyre'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."}

"\r\n\tQuery: INSERT INTO [TymaconOnline].[dbo].[Tyre] ([Eancode], [TyreTypeID], [CategoryID], [TyreBrandID], [SeasonID], [WheelPosID], [Width], [Height], [Wheelsize], [Radial], [Speed1], [Speed2], [LoadIndex1], [LoadIndex2], [Runflat], [Heavy], [Photolink], [Photolink_Large], [Photolink_Small], [Tltt], [StartingTread], [SilentTyreID], [BulkDefinition], [BacDefinition1], [BacDefinition2], [Blocked]) VALUES (@Eancode, @TyreTypeId, @CategoryId, @TyreBrandId, @SeasonId, @WheelPosId, @Width, @Height, @Wheelsize, @Radial, @Speed1, @Speed2, @LoadIndex1, @LoadIndex2, @Runflat, @Heavy, @Photolink, @PhotolinkLarge, @PhotolinkSmall, @Tltt, @StartingDepthMm, @SilentTyreId, @BulkDefinition, @BacDefinition1, @BacDefinition2, @Blocked)\r\n\tParameter: @Eancode : Decimal. Length: 0. Precision: 13. Scale: 0. Direction: Input. Value: 5420005502333.\r\n\tParameter: @TyreTypeId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1241.\r\n\tParameter: @CategoryId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 98.\r\n\tParameter: @TyreBrandId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 4.\r\n\tParameter: @SeasonId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 9.\r\n\tParameter: @WheelPosId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.\r\n\tParameter: @Width : Decimal. Length: 0. Precision: 6. Scale: 2. Direction: Input. Value: 10.00.\r\n\tParameter: @Height : Decimal. Length: 0. Precision: 6. Scale: 3. Direction: Input. Value: 75.000.\r\n\tParameter: @Wheelsize : Decimal. Length: 0. Precision: 6. Scale: 2. Direction: Input. Value: 153.00.\r\n\tParameter: @Radial : String. Length: 1. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.\r\n\tParameter: @Speed1 : String. Length: 5. Precision: 0. Scale: 0. Direction: Input. Value: \"08\".\r\n\tParameter: @Speed2 : String. Length: 5. Precision: 0. Scale: 0. Direction: Input. Value: \"\".\r\n\tParameter: @LoadIndex1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.\r\n\tParameter: @LoadIndex2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.\r\n\tParameter: @Runflat : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: False.\r\n\tParameter: @Heavy : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: False.\r\n\tParameter: @Photolink : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.\r\n\tParameter: @PhotolinkLarge : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.\r\n\tParameter: @PhotolinkSmall : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.\r\n\tParameter: @Tltt : AnsiString. Length: 5. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.\r\n\tParameter: @StartingDepthMm : Decimal. Length: 0. Precision: 18. Scale: 4. Direction: Input. Value: 0.0.\r\n\tParameter: @SilentTyreId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.\r\n\tParameter: @BulkDefinition : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: \"10.0/75-15.3 E3 IMPL 8PR TL 1\".\r\n\tParameter: @BacDefinition1 : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: \"10.0/75-15.3 DUNLOP E3\".\r\n\tParameter: @BacDefinition2 : AnsiString. Length: 200. Precision: 0. Scale: 0. Direction: Input. Value: \"8PR TL\".\r\n\tParameter: @Blocked : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: False.\r\n"

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Oct-2007 10:47:57   

Please post a complete code snippet.

Also check that field in the LLBLGen Pro Designer, and see if any of its "Is ReadOnly" or "Is Identity" flags are set to true.

raduch
User
Posts: 17
Joined: 26-Jan-2007
# Posted on: 30-Oct-2007 10:51:16   

Both flags are false.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Oct-2007 11:02:04   

Please post a complete code snippet.

raduch
User
Posts: 17
Joined: 26-Jan-2007
# Posted on: 30-Oct-2007 11:07:55   

Before the save i checked the IsNew and IsDirty flags, both true.

Thanks

... foreach (TyreBufferEntity tb in tyresToImport) { // if tyre is in the database it will be updated TyreEntity ty = new TyreEntity(tb.Bac);

            // copy tyre values from the buffer entity
            CopyTyreInfo(tb, ty);

            ty.Save();
        }

...

private static void CopyTyreInfo(TyreBufferEntity src, TyreEntity dest) { dest.Eancode = src.Eancode; dest.TyreTypeId = src.TyreTypeId; dest.CategoryId = src.CategoryId; dest.TyreBrandId = src.TyreBrandId; dest.SeasonId = src.SeasonId; dest.WheelPosId = src.WheelPosId; dest.Width = src.Width; dest.Height = src.Height; dest.Wheelsize = src.Wheelsize; dest.Radial = src.Radial; dest.Speed1 = src.Speed1; dest.Speed2 = src.Speed2; dest.LoadIndex1 = src.LoadIndex1; dest.LoadIndex2 = src.LoadIndex2; dest.Runflat = src.Runflat; dest.Heavy = src.Heavy; dest.Photolink = src.Photolink; dest.PhotolinkLarge = src.PhotolinkLarge; dest.PhotolinkSmall = src.PhotolinkSmall; dest.Tltt = src.Tltt; dest.StartingDepthMm = src.StartingDepthMm; dest.SilentTyreId = src.SilentTyreId; dest.BulkDefinition = src.BulkDefinition; dest.BacDefinition1 = src.BacDefinition1; dest.BacDefinition2 = src.BacDefinition2; dest.Blocked = src.Blocked; }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Oct-2007 11:17:41   

Please add the following line to the CopyTyreInfo method:

        private static void CopyTyreInfo(TyreBufferEntity src, TyreEntity dest)
        {
            dest.Bac= src.Bac;
            ...
        }
raduch
User
Posts: 17
Joined: 26-Jan-2007
# Posted on: 30-Oct-2007 11:58:05   

It works. Seemed normal to me that if I create an entity like TyreEntity ty = new TyreEntity(tb.Bac); the PK field will be set, but I was wrong.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Oct-2007 12:17:18   

Seemed normal to me that if I create an entity like TyreEntity ty = new TyreEntity(tb.Bac); the PK field will be set, but I was wrong.

The PK value passed to the constructor, is only used for fetching this entity, so if the entity is found in the database, the PK value will be set with the value fetched from the database (for sure it would be the same value passed in simple_smile ) But if the entity is not found the PK value won't be set.

This is by design, as some entities will have the PK as read-only (identity), so it shouldn't be set in the code, that's why the value passed to the constructor is only used for fetching.