create a new entityCollection if current collection is empty

Posts   
1  /  2
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 21-Mar-2006 21:14:04   

version 1.0.2005.1 final (self-servicing) VS2005 winforms


hiya, I think this should be a simple one but I can't find it in the help.

I have an entityCollection, I want to create a new entityCollection if the current collection is empty:


private myProject.CollectionClasses.TblDeliveryProductsCollection currDeliveries;

 if (currDeliveries.Count == 0)
            {

                currDeliveries = new TblDeliveryProductsCollection(); //hmm, I'm stuck :-(
                }

I need to create a new emptyCollection, so that I can fill it with entities.

This entityCollection contains an sqlServer identity column...

So, if the ID of the previous entityCollection was 3, then the ID of the newlyCreated entityCollection should be 4..make sense?

any help apprciated,

cheers,

          yogi
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 21-Mar-2006 21:20:20   

Not sure if I understand your problem exactly, but here goes.

An EntityCollection is a list of Entities. So in your cas the TblDeliveryProductsCollection contains a list of TblDeliveryProducts. A collection does not have an IdentityColumn, but the entities it contains might.


private myProject.CollectionClasses.TblDeliveryProductsCollection currDeliveries;
currDeliveries = new TblDeliveryProductsCollection(); 

//you now have an empty collection.  Put items in it with
currDeliveries.GetMulti();

//if you just want a single Entity use
TblDeliveryProducts deliveryProduct = new TblDeliveryProducts();

deliveryProduct.whatever = "Food";
deliveryProduct.Save();   //this will cause it to add a new delivery to the TblDeliveryProducts table (and it will load its new pkey identity value)

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 22-Mar-2006 01:11:52   

hiya Wayne,

Yes, I have done as you stated.But "save" causes an error because the product that I am adding needs a deliveryId.

<pseudo> IF current entityCollection empty THEN create a new entityCollection add a new product to this newly created entityCollection assign the deliveryId of this newly created entityCollection to the product save it. ELSE add a new product to this newly created entityCollection assign the deliveryId of this newly created entityCollection to the product save it. END

</pseudo>

Going with your code, I will show where it errors:


private myProject.CollectionClasses.TblDeliveryProductsCollection currDeliveries;
currDeliveries = new TblDeliveryProductsCollection(); 


currDeliveries.GetMulti(null);   //for some reason, I need to pass a NULL

TblDeliveryProducts deliveryProduct = new TblDeliveryProducts();

deliveryProduct.whatever = "Food";
deliveryProduct.deliveryId = ????? //are you saying that I don't have to add the PK value?
//If i don't add it, I get an error..that is why I assumed that the entityCollection would somehow have a PK value.

deliveryProduct.Save();

<error> Cannot insert the value NULL into column 'deliveryId', table 'dbC.dbo.tblDeliveryProducts'; </error>

<schema> tblProductDelivery deliveryId PK productId FK etc

tblProduct productId FK </schema>

does this make sense?

cheers, yogi


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Mar-2006 08:00:06   

I don't understand what you are trying to do.

If you are trying to create a new TblDeliveryProducts Entity and save then yes you have to assign all the non-Nullable fields before you save or else you would get that error.

Your main question was:

I have an entityCollection, I want to create a new entityCollection if the current collection is empty

I don't know if you want to fill the collection with entities from the database or you are trying to fill it with new Entities.

In the first case you would call GetMulti() In the first you will directly add Entities to the collection by using Add() method.

In either cases I don't see a reason to create a new collection, just use the empty one on hand.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 22-Mar-2006 21:29:51   

hiya Walaa,

I don't see a reason to create a new collection, just use the empty one on hand.

Point taken.

I don't know if you want to fill the collection with entities from the database or you are trying to fill it with new Entities.

I am trying to fill it with NEW entities.

I still receive the SAME error as before. It is STILL looking for the deliveryId....

You suggest that I don't have to manually assign it, which makes sense, as it hasn't yet been created in the underlying database.

How do I save the entityCollection to the database, in such a way that it automatically assigns the deliveryId of the newly added product?

Many thanks,

yogi


currDeliveries.GetMulti(null);
            TblProductEntity prodEntity = new TblProductEntity(txtBarcode.Text);
            TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
           // newProduct.DeliveryId = 0;   //WHERE does it get the deliveryId from??????
            newProduct.BarCode = txtBarcode.Text;
            newProduct.Qty = 1;
            newProduct.UnitPrice = prodEntity.UnitPrice;

            currDeliveries.Add(newProduct);
            currDeliveries.SaveMulti(true);  //ERROR ...can't assign NULL ti product deliveryId




bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 23-Mar-2006 02:08:57   
currDeliveries.GetMulti(null);
TblProductEntity prodEntity = new TblProductEntity(txtBarcode.Text);
TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
// newProduct.DeliveryId = 0; //WHERE does it get the deliveryId from??????
newProduct.BarCode = txtBarcode.Text;
newProduct.Qty = 1;
newProduct.UnitPrice = prodEntity.UnitPrice;

currDeliveries.Add(newProduct);
currDeliveries.SaveMulti(true);  //ERROR ...can't assign NULL ti product deliveryId

Ok, the DeliveryId is intialized to 0 because it is probably an int type which can't be null. So a new entity has a default value of 0, but it's really a null value, newProduct.DeliveryId.IsNull is true. Adding the newProduct to a collection of other TblProductEntities will not assign it a deliverId. To do this you would need to set something like newProduct.TblDelivery = an instance of a TblDelivery, or set the DeliveryId directly. If it is a new Delivery then do something like this.

TblDeliveryEntity delivery = new TblDeliveryEntity();
TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
newProduct.TblDelivery = delivery;
newProduct.BarCode = txtBarcode.Text;
newProduct.Qty = 1;
newProduct.UnitPrice = prodEntity.UnitPrice;

delivery.Save(true);

Let us know if this doesn't help or if you need more explanation on pieces.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 23-Mar-2006 19:04:49   

cheers Bclubb,

To do this you would need to set something like newProduct.TblDelivery = an instance of a TblDelivery, or set the DeliveryId directly. If it is a new Delivery then do something like this.

Ok, I tried the code you suggested, but "newProduct" does not expose a "TblDelivery " property.I obviously can't set the deliveryId directly, as I don't yet know what it is.

I will post the code that you included, and include my own additional lines..I think you might have omitted a few lines.Ideally could you include all the lines?

..Sorry to ask, it's just that it's very confusing for me to know whether you intended to include a code snip, OR, whether the code that you posted is all that I should need.

Many thanks,

yogi



TblProductEntity prodEntity = new TblProductEntity(txtBarcode.Text); //ADDED line..

TblDeliveryEntity delivery = new TblDeliveryEntity();
TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
newProduct.TblDelivery = delivery;   //ERROR, doesn't expose a "TblDelivery " property.
newProduct.BarCode = txtBarcode.Text;
newProduct.Qty = 1;
newProduct.UnitPrice = prodEntity.UnitPrice;

delivery.Save(true);

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Mar-2006 08:13:41   

"newProduct" does not expose a "TblDelivery " property

Ain't there a relation between those 2 entities defined in the database & in the LLBLGen Pro Designer. ?

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 24-Mar-2006 12:39:14   

hiya Walaa,

The relations are now fine and I can now access the "TblDelivery " property.

Fine, but the deliveryId is still not implicitly set.

I still get the error:

<error> Cannot insert the value NULL into column 'deliveryId', table </error>


TblProductEntity prodEntity = new TblProductEntity(); 
            TblDeliveryEntity delivery = new TblDeliveryEntity();
    
            int delId;
            delId = delivery.DeliveryId;   //the deliveryId is ZERO, I thought it would  have been the next available sqlServer identity number???

            TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();
            newProduct.TblDelivery = delivery; //No error here now                      
            newProduct.ProductId = 4;            //valid productId   

            delivery.Save(true);      //ERROR

Now, I don't know why this errors. I looked at my sql tables..and they are set up as follows:

<schema>

tblDelivery deliveryId PK identity column

tblProduct productId PK identity column productName

tblDeliveryProducts deliveryId candidate PK (related to tblDelivery.deliveryId) productId candidate PK (related to tblProduct.productId) </schema>

I believe that I have correctly set up my sqlServer tables...HOW can I tell WHY the deliveryId is not set to the next available deliveryId when I try to save?

many thanks,

yogiberr

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Mar-2006 16:05:50   

You have to set it the same way explained by Brian:

TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
newProduct.TblDelivery = delivery; // entity instance assigned.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 24-Mar-2006 16:49:04   

OK, Sorry to drag this one out :=0

I have done it EXACTLY as Brian suggested:


TblDeliveryEntity delivery = new TblDeliveryEntity();
TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
newProduct.TblDelivery = delivery;           //using a watch, the deliveryId is ZERO
newProduct.BarCode = txtBarcode.Text;   // VALID
newProduct.Qty = 1;                                // VALID     
newProduct.UnitPrice = 24                         // VALID
delivery.Save(true);                                     //ERROR...it can't find the deliveryId, which should be implied from the newly created "delivery" object??

If I hard-code the "newProduct.deliveryId", then it works...thereby proving that the ONLY problem I have is with the delivery.deliveryId

Please remember that "TblDeliveryProductsEntity" has 2 keys as its primary key. Both of these keys are actually foreign keys for identity fields in other tables....does this shed any light?

The real questions are: 1) how "should" the tblDelivery.deliveryId identity field be assigned to my deliveryProduct entity? 2) How can I find out "where" it is going wrong?

many thanks,

yogi

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 24-Mar-2006 23:23:25   

hiya Walaa

TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity(); newProduct.TblDelivery = delivery; // entity instance assigned.

Ok, are you saying that (if I run the code and use a "watch") then newProduct.TblDelivery.deliveryId SHOULD be the next available sqlServer identityField value?

Because the value that I see at run-time is "0", suggesting that the value has not been successfully obtained.I still get the same invalid deliveryId message as before.

I know it's the weekend, but I'm hoping that someone can tell me why the newly created delivery entity does not contain the next identityField value...

At this stage, I can't be sure whether it's:

1) schema design problems 2) llblGenpro designer settings that I haven't correctly set

or something else...By the way, I have refreshed the database catalog, so I can rule that out as a potential problem...But that is ALL that I can rule out.

I'm sure that loads of other people must have successfully done what I am trying to do. Why does the delivery.deliveryId not auto increment?I have been thru all the help.

Please help,

yogi

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 25-Mar-2006 01:25:40   

simple_smile

The value is 0 initially because it is an int and must have a value. Once the Delivery is saved to the database then it will be assigned a value and using scope_identity LLBLGen will know what id is assigned. Then when it gets to saving the products through recursion it should assign this deliveryid. Can you post your trace of the sql that is being generated when the save is executed? To capture the sql trace look at the troubleshooting portion of the documentation.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Mar-2006 02:16:29   

DeliveryID will not be set until it is persisted to the database.

Stepping back for a moment and thinking about your problem from an OO perspective, You should create a new Delivery. Because of the relationship between Delivery and DeliveryProducts, there should already be an empty DeliveryProducts collection linked to the Delivery.

Don't worry about setting the DeliveryID directly. Set the object references, the keys will take care of themselves.

Therefore your code should look something like this:


TblDeliveryEntity delivery = new TblDeliveryEntity();

TblDeliveryProductsEntity newProduct = delivery.TblDeliveryProductsEntity();
// assigns newProduct delivery so that FK relationship is maintained
newProduct.TblDelivery = delivery;
newProduct.BarCode = txtBarcode.Text;
newProduct.Qty = 1;
newProduct.UnitPrice = 24;

// adds it to the existing, initially empty collection
delivery.tblDeliveryProducts.Add(newproduct);
delivery.Save(true);

From the manual:

Generated code - Using the entity classes, SelfServicing

Instantiate a Customer entity, add a new Order object to its Orders collection. Now add OrderDetails objects to the new Order object's OrderDetails collection,. You can simply save the Customer entity and all included new/'dirty' entities will be saved and any PK-FK relations will be updated/synchronized automatically. "

FK-PK synchronization This synchronization of FK-PK values is already done at the moment you set a property to a reference of an entity object, for example myOrder.Customer = myCustomer, if the entity (in this case myCustomer) is not new. Synchronization is also performed after a save action, so identity/sequenced columns are also synchronized.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 25-Mar-2006 11:35:31   

hiya folks,

ta for the replies. I think I'm nearly there :-)

<sqlTrace> exec sp_executesql N'SELECT [dbo].[tblDeliveryProducts].[deliveryId] AS [DeliveryId], [dbo].[tblDeliveryProducts].[barCode] AS [BarCode], [dbo].[tblDeliveryProducts].[unitPrice] AS [UnitPrice], [dbo].[tblDeliveryProducts].[qty] AS [Qty] FROM [dbo].[tblDeliveryProducts] WHERE ( ( [dbo].[tblDeliveryProducts].[deliveryId] = @DeliveryId1))',N'@DeliveryId1 int',@DeliveryId1=0 </sqlTrace>


TblDeliveryProductsEntity newProduct = delivery.TblDeliveryProductsEntity();

Now, delivery does not expose the "TblDeliveryProductsEntity" property.

I have: 1) checked the relationships both in sqlServer and llbl designer, they seem fine. 2) refreshed the catalogs, just in case.

<relationshipsInLlbDesigner> tblDelivery -TblDeliveryProducts (1:n). field mapped: TblDeliveryProducts -->tblDelivery.deliveryId (1:n). TblDeliveryProducts.deliveryId </relationshipsInLlbDesigner>

Does this explain why the delivery does not expose the "TblDeliveryProductsEntity" property?

I'm confused, but hopeful, and in a hurry :-0

many thanks, yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Mar-2006 14:29:22   

Is it possible that the relationship is backwards?

It would seem to be that one Delivery may have multiple products delivered.

tblDelivery-->TblDeliveryProducts

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 25-Mar-2006 15:59:37   

hiya,

Yes, a single delivery may contain multiple products. It could be thought of as an order, containing many products.

eg..a single delivery

tblDelivery deliveryId 1

tblDeliveryProducts deliveryId 1 1 1 productId 0 1 2

Does this shed any light?

many thanks,

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Mar-2006 16:07:07   

Yes, that matches my understanding.

What it does not explain is why tblDelivery does not have a tblDeliveryProducts collection.

The only ways that I know that can happen is if a relationship has not been defined in the database, the relationship is backwards or it has been hidden in the designer.

Please verify the relationship definitions and post the DDL of those two tables including relationships.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 25-Mar-2006 16:49:37   

cheers Jim,

1) the relationship is not hidden in the designer. 2) i'm not sure what a backwards relation is.

Below is the DDL.

many thanks,

yogi

<DDL_tblDeliveryProducts> CREATE TABLE [dbo].[tblDeliveryProducts]( [deliveryId] [int] NOT NULL, [barCode] nchar COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [unitPrice] [money] NULL, [qty] [int] NOT NULL, CONSTRAINT [PK_tblDeliveryProducts] PRIMARY KEY CLUSTERED ( [deliveryId] ASC, [barCode] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

GO USE [dbC] GO ALTER TABLE [dbo].[tblDeliveryProducts] WITH CHECK ADD CONSTRAINT [FK_tblDeliveryProducts_tblDelivery] FOREIGN KEY([deliveryId]) REFERENCES [dbo].[tblDelivery] ([deliveryId]) GO ALTER TABLE [dbo].[tblDeliveryProducts] WITH CHECK ADD CONSTRAINT [FK_tblDeliveryProducts_tblProduct] FOREIGN KEY([barCode]) REFERENCES [dbo].[tblProduct] ([barCode])

</DDL_tblDeliveryProducts>

<DDL_tblDelivery> CREATE TABLE [dbo].[tblDelivery]( [deliveryId] [int] IDENTITY(0,1) NOT NULL, [deliveryDate] [datetime] NULL, [runId] [int] NOT NULL, [driverId] [int] NOT NULL, CONSTRAINT [PK_tblDelivery] PRIMARY KEY CLUSTERED ( [deliveryId] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

GO USE [dbC] GO ALTER TABLE [dbo].[tblDelivery] WITH CHECK ADD CONSTRAINT [FK_tblDelivery_tblDriver] FOREIGN KEY([driverId]) REFERENCES [dbo].[tblDriver] ([driverId]) GO ALTER TABLE [dbo].[tblDelivery] WITH CHECK ADD CONSTRAINT [FK_tblDelivery_tblRun] FOREIGN KEY([runId]) REFERENCES [dbo].[tblRun] ([runId]) <DDL_tblDelivery>

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Mar-2006 18:10:10   

I could not get your DDL script to execute properly, so I created a similar one and generated a self servicing C# project.

It did create the tblDeliveryProducts collection property on tblDelivery when I generated it.

This code compiles and works.

I did notice in your DDL that you had a starting value of 0, I would recommend 1.


CREATE TABLE [dbo].[tblDelivery] (
    [deliveryID] [int] IDENTITY (1, 1) NOT NULL ,
    [deliverydate] [datetime] NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[tblDeliveryProduct] (
    [deliveryID] [int] NOT NULL ,
    [barCode] [nchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblDelivery] ADD 
    CONSTRAINT [PK_tblDelivery] PRIMARY KEY  CLUSTERED 
    (
        [deliveryID]
    )  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[tblDeliveryProduct] ADD 
    CONSTRAINT [PK_tblDeliveryProduct] PRIMARY KEY  CLUSTERED 
    (
        [deliveryID],
        [barCode]
    )  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[tblDeliveryProduct] ADD 
    CONSTRAINT [FK_tblDeliveryProduct_tblDelivery] FOREIGN KEY 
    (
        [deliveryID]
    ) REFERENCES [dbo].[tblDelivery] (
        [deliveryID]
    )
GO


private void button1_Click(object sender, EventArgs e)
{
// tblDeliveryCollection1 was added to a WinForm using the designer
    Yogi.DAL.EntityClasses.TblDeliveryEntity delivery = 
(Yogi.DAL.EntityClasses.TblDeliveryEntity) this.tblDeliveryCollection1.AddNew();
// I did need to set at least one property on the Delivery in order to avoid a null deliveryid error.
    delivery.Deliverydate = DateTime.Now;
    
    Yogi.DAL.EntityClasses.TblDeliveryProductEntity deliveryproduct = new Yogi.DAL.EntityClasses.TblDeliveryProductEntity();
    deliveryproduct.TblDelivery = delivery;
    deliveryproduct.BarCode = "sample";
    
    Yogi.DAL.CollectionClasses.TblDeliveryProductCollection deliveryproducts = delivery.TblDeliveryProduct;
    deliveryproducts.Add((SD.LLBLGen.Pro.ORMSupportClasses.IEntity) deliveryproduct);
    delivery.Save(true);
}

I'm shutting down for the day to do family stuff, but will check back again on Sunday.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 26-Mar-2006 14:18:34   

hiya Jim,

1st things 1st, this hasn't become quite so urgent...enjoy the weekend, I'm just posting this just now in the hope that I can see where I'm going wrong :-)

I used the code, but I get a cast error:

Unable to cast object of type 'myProject.EntityClasses.TblDeliveryProductsEntity' to type 'myProject.EntityClasses.TblDeliveryEntity'.


Yogi.DAL.EntityClasses.TblDeliveryEntity delivery = 
(Yogi.DAL.EntityClasses.TblDeliveryEntity) this.tblDeliveryCollection1.AddNew();

Now, I know WHY this errors...It is because I am binding the datagrid to a "deliveryProducts" entity collection, NOT to a "delivery" entity collection.This is the issue that I have to solve..I somehow have to associate the new deliveryProductEntity with a new deliveryconfused

As you know, the delivery entity is the key to everything, it contains the identity column that I need. I tried to create the delivery as a new TblDeliveryEntity, but obviously that didn't work :-(

So, that's where I am.

Any suggestions?

cheers,

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 26-Mar-2006 20:16:52   

Now, I know WHY this errors...It is because I am binding the datagrid to a "deliveryProducts" entity collection, NOT to a "delivery" entity collection.This is the issue that I have to solve

You are correct, that is where the problem lies.

I understand you want to display the DeliveryProducts. I believe the secret lies in the DataMember property of the DataSource.

In my example, I dragged a Button, a DataGridView, a BindingSource and a tblDeliveryCollection onto my WinForm.

I then went to the DataSource propertygrid item of the DataGridView. Selected the dropdown arrow and navigated to BindingSource1 and then down to tblDeliveryProduct.

Upon completing that, the designer added a new BindingSource called "tblDeliveryProductBindingSource" that set the DataMember and DataSource properties. Following code was pulled out of the VS2005 designer generated code.


// 
// bindingSource1
// 
this.bindingSource1.DataSource = this.tblDeliveryCollection1;
...snip...
// 
// tblDeliveryProductBindingSource
// 
this.tblDeliveryProductBindingSource.DataMember = "TblDeliveryProduct";
this.tblDeliveryProductBindingSource.DataSource = this.bindingSource1;
...snip...
// 
// dataGridView1
// 
...snip...
this.dataGridView1.DataSource = this.tblDeliveryProductBindingSource;

In my earlier example, I added the delivery and a product all in one click event. Not required. Creating a delivery really depends on the flow of your user interface. One way you may want to do it is to create a new delivery in the constructor of the form and create a new form for each delivery. And then let the user create as many DeliveryProduct entries as required.

1st things 1st, this hasn't become quite so urgent...enjoy the weekend, I'm just posting this just now in the hope that I can see where I'm going wrong :-)

I appreciate that. For the record, I am also an end user of LLBLGen Pro and don't work for them. My answers are complety unofficial!

Trying to answer questions here helps me learn about features of the product that I may not have otherwise explored and hopefully frees up Frans and the other folks so they can focus on getting 2.0 out the door! simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 27-Mar-2006 12:05:39   

JimHugh wrote:

I appreciate that. For the record, I am also an end user of LLBLGen Pro and don't work for them. My answers are complety unofficial!

But well appreciated! simple_smile

Trying to answer questions here helps me learn about features of the product that I may not have otherwise explored and hopefully frees up Frans and the other folks so they can focus on getting 2.0 out the door! simple_smile

Thanks for that, Jim! smile .

Frans Bouma | Lead developer LLBLGen Pro
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 27-Mar-2006 21:35:35   

righto Jim,

I apologise in advance, but there are yet more questions.Please bear in mind that the entire justification for this application is barcoding, where I can:

1) grab the primary key of a product entity, 2) add it to a deliveryProductEntity 3) implicitly assign the deliveryId of the "parent" tblDeliveryEntity. which contains the identity field that I need.

I grab the barcode from a text_changed event of a textbox. In other words, I am NOT directly creating entities in the datgridview, more's the pity :-(

, I dragged a DataGridView, a BindingSource and a tblDeliveryCollection onto my WinForm

ok, I follow you.

Now, all the fields are correct in my datagridView.

I can populate my datagridView as follows:

   tblDeliveryCollection1.GetMulti(null);
    

so far so good.

In my earlier example, I added the delivery and a product all in one click event. Not required. Creating a delivery really depends on the flow of your user interface. One way you may want to do it is to create a new delivery in the constructor of the form and create a new form for each delivery. And then let the user create as many DeliveryProduct entries as required.

right, this is where I'm losing it.I've been thru a lot of code permutations (as you know :-) )

Let's say for arguments sake that I need to create a new delivery..as you know, I now have both:

1) tblDeliveryCollection1 2) tblDeliveryProductsBindingSource

..all nicely set up. Now, the code below is a confused mix of my understanding so far. All I want to do is create a new delivery (autoInc) Add a deliveryproduct to this delivery.

The code causes the same error as before,

cannot insert the value NULL into column deliveryId of tblDeliveryProducts

Any suggestions?

phew, many, many thanks,

yogi

   tblDeliveryCollection1.GetMulti(null);
           TblProductEntity prodEntity = new TblProductEntity(txtBarcode.Text);
            TblDeliveryEntity delivery =  (TblDeliveryEntity)tblDeliveryCollection1.AddNew();  
            TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();
            newProduct.TblDelivery = delivery;
            newProduct.BarCode = txtBarcode.Text;
            newProduct.Qty = 222;
            newProduct.UnitPrice = prodEntity.UnitPrice;

            TblDeliveryProductsCollection deliveryproducts = delivery.TblDeliveryProducts;
            deliveryproducts.Add((SD.LLBLGen.Pro.ORMSupportClasses.IEntity)newProduct);
    
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 27-Mar-2006 22:19:48   

Must have missed an embedded comment in my earlier code sample...


// I did need to set at least one property on the Delivery in order to avoid a null deliveryid error.
    delivery.Deliverydate = DateTime.Now;

So be sure to set at least one property of the delivery to an appropriate value, otherwise LLBLGen will not attempt to persist the delivery.

From the docs:

Option 1 is likely the most used one, since an entity might already be in memory. As with all the suggested options, the Save() method will see that the entity isn't new, and will therefore use an UPDATE query to alter the entity's data in the persistent storage. An UPDATE query will only update the changed fields in an entity that is saved, which results in efficient queries. If no fields are changed, no update is performed. A field which is set to the same value (according to Equals()) is not marked as 'changed' (i.e. the field's IsChanged flag is not set).

1  /  2