The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Adding child rows to a new parent

Last post 07-12-2007, 4:37 PM by David.Parsons. 4 replies.
Sort Posts: Previous Next
  •  07-11-2007, 1:21 PM 3804

    Adding child rows to a new parent

    I have two separate grids on my form, a parent grid and a child grid. The parent grid's data structure has an identity column and the child incorporates that field as part of its primary key. Both grid's datasources are pointed to bindingSources that point to ES collections.

    When the parent record changes I load the corresponding child records. 

    Code:
                //The parent record has changed. Load the new child details
                User usr = new User();
                usr = (User)userCollectionBindingSource.Current;
                userCustomerLocationsCollection1 = usr.UserCustomerLocationsCollectionByUserId;
                bindingSourceUserCustLocations.DataSource = userCustomerLocationsCollection1;
    

    If I add a new parent record, its identity field is not set; when I move directly to the child record, how should I set the key field as the parent's value is not yet set. Is there a best method for doing this?

  •  07-11-2007, 7:20 PM 3809 in reply to 3804

    Re: Adding child rows to a new parent

    Well, since it's an identity column you might cheat, we do keep it writeable so you can assign a value to it as long as it doesn't conflict with an existing value, maybe you can set it to the maximum int value and keep track of it on your form incase they add another one, then you'd have to decrement it by one. Of course, the value will not be used when you call save which is good. You might try that, just an idea but I bet you can get it to work.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  07-12-2007, 1:13 AM 3811 in reply to 3804

    Re: Adding child rows to a new parent

    Hi

    I'm assuming that if you're at the stage of adding a child record then you're happy with the parent record and therefore it could be saved so why not check the state of the parent record in your code to add/update children and if the parent is new/added, call save on the parent - then I assume your current problem is no longer an issue?

    Just an idea

    Cheers

    Martin

  •  07-12-2007, 3:32 PM 3818 in reply to 3809

    Re: Adding child rows to a new parent

    I have to get this to work with the Infragistics grid so I am not sure how relevant this is but one question. Let's say I set the parent PK to -1 and the PK field in the child record also to -1. Then the save takes place the parent PK might be set to 100, will a save of the child records also automatically set their PK field to 100 or would it be left at -1 since it in not an identity field?

     I think the dataset lets you save parent and child records like this. It keeps a track of those disconnected values and then sets them to real values when the rows are added to the database.

  •  07-12-2007, 4:37 PM 3819 in reply to 3818

    Re: Adding child rows to a new parent

    When you call Save(), EntitySpaces will retrieve the identity key, and fill it in to the FK column for all related child records, then save them as well.
    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML