Re-Sort Collection After Delete

Posts   
 
    
larkydoo
User
Posts: 45
Joined: 30-Jun-2008
# Posted on: 12-Jul-2013 18:50:52   

Using LLBLGenPro v2.6

I am running into odd issues when I delete an entity from collection, then try to re-sort the collection. In the re-sort, it thinks the collection still contains the entity in question. I am using Ajax, which I don't imagine should be a problem but thought it worth mentioning.

Anyway, here's my code:

  1. Delete the Section and the Fields under it:
    protected void hlRemoveSection_Click(object sender, EventArgs e)
    {
        LinkButton myButton = (LinkButton)sender;
        StageSectionEntity mySection = new StageSectionEntity(Convert.ToInt32(myButton.CommandArgument));
        FormStageEntity myFormStage = mySection.FormStage;
        Session["MasterPageMessage"] = "Section " + mySection.FormSectionTitle + " deleted.";
        mySection.StageSectionField.DeleteMulti();  
        mySection.Delete();
        myFormStage.ReSortSections();
        rgGrid.Rebind();
    }

  1. ReSortSections Method in parent Entity User code region:

        public void ReSortSections()
        {
            StageSectionCollection myStageSections = StageSectionCollection.GetSortedSectionsByFormStage(this.FormStageId);
            int i = 0;
            foreach (StageSectionEntity mySection in this.StageSection) 
            {
                i++;
                mySection.SortOrder = i;
                mySection.Save();
            }
        }

  1. GetSortedSectionsByFormStage:

        public static StageSectionCollection GetSortedSectionsByFormStage(int FormStageID)
        {
            StageSectionCollection mySections = new StageSectionCollection();
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(StageSectionFields.FormStageId == FormStageID);
            SortExpression sorter = new SortExpression();
            sorter.Add(StageSectionFields.SortOrder | SortOperator.Ascending);
            mySections.GetMulti(filter, 0, sorter, null);
            return mySections;
        }

For some reason, the GetSortedSectionsByFormStage method is returning all records, including the one that was deleted.

Thanks for any help you can provide.

Laurie

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Jul-2013 07:42:46   

Hi Laurie,

larkydoo wrote:

        public void ReSortSections()
        {
            StageSectionCollection myStageSections = StageSectionCollection.GetSortedSectionsByFormStage(this.FormStageId);
            int i = 0;
            foreach (StageSectionEntity mySection in this.StageSection) 
            {
                i++;
                mySection.SortOrder = i;
                mySection.Save();
            }
        }

I don't understand above code. You are getting a brand new fresh collection "myStageSections" but you never use it further in your code, you are just sorting another collection " this.StageSection". I think that is the problem. Maybe your code should look like:

        public void ReSortSections()
        {
            this.StageSection = StageSectionCollection.GetSortedSectionsByFormStage(this.FormStageId);
            int i = 0;
            foreach (StageSectionEntity mySection in this.StageSection) 
            {
                i++;
                mySection.SortOrder = i;
                mySection.Save();
            }
        }
David Elizondo | LLBLGen Support Team
larkydoo
User
Posts: 45
Joined: 30-Jun-2008
# Posted on: 18-Jul-2013 18:58:51   

David,

It doesn't matter how I retrieve the collection. I can use the following code:

        public static StageSectionCollection GetSortedSectionsByFormStage(int FormStageID)
        {
            StageSectionCollection mySections = new FormStageEntity(FormStageID).StageSection;// new StageSectionCollection();
            mySections.Sort(StageSectionFields.SortOrder.FieldIndex, ListSortDirection.Ascending);
            //IPredicateExpression filter = new PredicateExpression();
            //filter.Add(StageSectionFields.FormStageId == FormStageID);
            //SortExpression sorter = new SortExpression();
            //sorter.Add(StageSectionFields.SortOrder | SortOperator.Ascending);
            //mySections.GetMulti(filter, 0, sorter, null);
            return mySections;
        }

and the same thing happens. The record I just deleted shows up as the first record in the returned collection in the ReSort method, which messes up my sort. When I run a trace on the collection, I get the following for the first record in the collection:

mySection.SortOrder 'mySection.SortOrder' threw an exception of type 'SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityIsDeletedException' int {SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityIsDeletedException}

It's as if the record I just deleted is not completely deleted from the collection.

The reason I am using a static method is because I'd hoped that by retrieving the collection "fresh," I would get only non-deleted records. This is not the case. Normally I would just get this.StageSection and run a sort on that, then update the sortorders.

Thanks for your help!

Laurie

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Jul-2013 20:23:27   

First things first, which runtime library version are you using? (build no.) Then, please try David's code, and make sure you fetch from the database, and apply the sort on the client side on the collection just fetched.

Also please check enable DQE tracing, and check to see if the Delete SQL Command is indeed fired, and fired with the correct arguments.

Then before jumping into the refetch/resort section, check the database to see if the record is actually deleted from the database.

larkydoo
User
Posts: 45
Joined: 30-Jun-2008
# Posted on: 18-Jul-2013 21:30:29   

Running v2.6 Final. Not seeing a build #.

I think I just figured it out. In the item deletion code:

    protected void hlRemoveSection_Click(object sender, EventArgs e)
    {
        LinkButton myButton = (LinkButton)sender;
        StageSectionEntity mySection = new StageSectionEntity(Convert.ToInt32(myButton.CommandArgument));
        FormStageEntity myFormStage = mySection.FormStage;
        Session["MasterPageMessage"] = "Section " + mySection.FormSectionTitle + " deleted.";
        mySection.StageSectionField.DeleteMulti();  
        mySection.Delete();
        myFormStage.ReSortSections();
        rgGrid.Rebind();
    }

I was initializing myFormStage based on mySection. When mySection is deleted, myFormStage becomes unknown and chokes on the ReSort. I changed the code to


    protected void hlRemoveSection_Click(object sender, EventArgs e)
    {
        LinkButton myButton = (LinkButton)sender;
        StageSectionEntity mySection = new StageSectionEntity(Convert.ToInt32(myButton.CommandArgument));
        int intFormStageID = mySection.FormStage.FormStageId;
        Session["MasterPageMessage"] = "Section " + mySection.FormSectionTitle + " deleted.";
        mySection.StageSectionField.DeleteMulti();

        mySection.Delete();
        FormStageEntity myFormStage = new FormStageEntity(intFormStageID);
        myFormStage.ReSortSections();
        rgGrid.Rebind();
    }

and it worked like a charm.

I have other issues, but I think they're just bad code on my part, so I'll just muddle through.

Thanks!

Laurie