Strange issue saving child objects

Strange issue saving child objects

Old forum URL: forums.lhotka.net/forums/t/12695.aspx


Brendt posted on Monday, July 07, 2014

Good day.

i have a strange issue, i have a quotation (invoice) form with child lines. if i add new lines and save it works perfectly, if i delete a line and save this too works fine. but i find that if i delete a couple if lines click save and then add new lines and click save the lines the deleted item are not deleted properly and the new lines are just saved to the database. what could be causing this issue? am i supposed to manually clear my deleted lines list at some point?i also fined that if i have 4 saved lines to begin with and click save that all 4 lines are saved, if i then remove 2 lines and save 2 lines are removed from the database, but if i then click add to create a new line and click save the deleted lines are put back and saved to the database. i should only have 3 lines in the table but i end up with  5 lines (2 original lines, 1 newly inserted line and the 2 lines previously deleted)

i have this code in my QuotationFactory:

public void UpdateChildren(QuotationLineList obj, Quotation q)
        {
          
            foreach (var item in GetDeletedList<QuotationLine>(obj))
            {
                this.Update(item, q);
            }

            foreach (QuotationLine item in obj)
            {
                this.Update(item, q);
            }
        }

which calls the following:

public QuotationLine Update(QuotationLine obj, Quotation q)
        {

            if (obj.IsDeleted)
            {
                if (!obj.IsNew)
                {
                    ExecuteDelete(obj.ID);
                
                }
                MarkNew(obj);
            }
            else
            {
                if (obj.IsNew)
                {
                    //Insert New data
                    obj.QUID = q.Id;
                    ExecuteInsert(obj);
                   
                }
                else
                {
                    //Update existing data
                    ExecuteUpdate(obj);
                  
                }
            }

           
          
            return obj;
        }

any help regarding this would be much appreciated.

 

 

JonnyBee replied on Monday, July 07, 2014

Hi,

Yes, your code must call clear on the deleted list.

Brendt replied on Tuesday, July 08, 2014

Hi JonnyBee.

Thank you for your reply. How would i clear the deleted list? is this ok:

GetDeletedList<QuotationLine>(obj).Clear();

also i would like to ask another question, when i open my quotation form i call BeginEdit and then after every change i click save and call ApplyEdit, is this the right way to to this? i do not receive any "Object is still being edited" errors and every new change is saved, do i need to call BeginEdit before adding new lines?

Copyright (c) Marimer LLC