Always refetch

Posts   
 
    
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 13-May-2005 04:00:45   

I have learned the hard way to always refetch after persisting anything in the adapter scenario. I already read and understood Frans' explanation of why to do this, but without really thinking about it I was cutting a corner and it caused me some grief, though I am not sure why, but refetching solved the problem.

I've got a Janus ASP.NET grid. After adding a record, the grid would act really goofy. Let's say I add one record. It's a PO line item that is numbered 1 by my code. After the save, I would have 3 rows - first row has the line number 1, second has the remaining fields, and the 3rd row was ready for new input. Of course rows 1 and 2 here should just be the same. In the appropriate event handler for the grid I was doing this:


            EntityCollection details = po.PODetail;
            PODetailEntity detail = new PODetailEntity();
            GridEXCellCollection cells = e.Row.Cells;

            try
            {
                detail.PONumber = po.PONumber;
                detail.POLineNumber = (short)(details.Count + 1);
                detail.Item = cells["Item"].Text;
                detail.AcctID = cells["AcctID"].Text;
                detail.Quantity = (float)(double)cells["Quantity"].Value;
                detail.UOMID = (int)cells["UOMID"].Value;
                detail.Price = (float)(double)cells["Price"].Value;
                detail.TaxRate = (float)(double)cells["TaxRate"].Value;
                if (cells["QuoteReference"].Text != "")
                    detail.QuoteReference = cells["QuoteReference"].Text;
                mgr.SaveDetail(detail);
                details.Add(detail);

I was attempting to cut a little corner by just adding the new line item directly to my collection. Replacing


                details.Add(detail);

with


                mgr.FetchPO(po.PONumber);

magically solved my problem, though like I said, I don't know why. The details collection (which I rebind in another event handler) has the right count, so this seemed like a harmless thing to do.

Maybe this post will save someone else a few hours. cry

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 13-May-2005 11:42:09   

saving an entity makes it outofsync with the db, so if you're planning to keep on using that entity in a scenario in which data from that entity is read AFTER the save (and in this case, it is, as data is read to fill the cells), you have to refetch it with the save. You can do that in one go, just state with the saveentity call that you want to refetch the entity, and it's done. You can then proceed as normal by adding the entity to the grid.

Frans Bouma | Lead developer LLBLGen Pro
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 13-May-2005 15:13:14   

Otis wrote:

saving an entity makes it outofsync with the db, so if you're planning to keep on using that entity in a scenario in which data from that entity is read AFTER the save (and in this case, it is, as data is read to fill the cells), you have to refetch it with the save. You can do that in one go, just state with the saveentity call that you want to refetch the entity, and it's done. You can then proceed as normal by adding the entity to the grid.

Ok, thanks. If I have refetched the detail entity, I assume that it is in fact ok for me to just add it to the detail collection? Obviously if I refetched the entire PO (with the appropriate prefetch path defined) the collection would now have all the records, but if I just refetch the one new detail after inserting it, I can get away with just adding it to the collection myself?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 13-May-2005 17:29:35   

JimFoye wrote:

Otis wrote:

saving an entity makes it outofsync with the db, so if you're planning to keep on using that entity in a scenario in which data from that entity is read AFTER the save (and in this case, it is, as data is read to fill the cells), you have to refetch it with the save. You can do that in one go, just state with the saveentity call that you want to refetch the entity, and it's done. You can then proceed as normal by adding the entity to the grid.

Ok, thanks. If I have refetched the detail entity, I assume that it is in fact ok for me to just add it to the detail collection?

Yes. Just refetch entities you save (using the flag on SaveEntity), this avoids refetching data which isn't out of sync.

Obviously if I refetched the entire PO (with the appropriate prefetch path defined) the collection would now have all the records, but if I just refetch the one new detail after inserting it, I can get away with just adding it to the collection myself?

Yes, if that's the entity just saved and I understand your code correctly. simple_smile

Frans Bouma | Lead developer LLBLGen Pro