Deleting Record from a BO


Author
Message
Scott
Scott
StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)
Group: Forum Members
Posts: 176, Visits: 1.5K
I have a BO filled with 3 records.  I am navigating thru the records with the following logic:

bo.fill();
if (bo.MoveFirst())
   do
   {
       bo.DeleteCurrentRow();
   } while(bo.MoveNext());

One record is always skipped becuase when the first record is delete the currentrow index is decreased to -1 then the bo is navigated which sets the currentrow to 0, when the call to MoveNext is made currentrow is increated to 1 there by skipping the second record in the table.

Being from a FoxPro background I was looking at this structure as a replacement for the SCAN ENDSCAN construct.  I can see that it would work for looping thru records easily but what do you recommend when looping and doing deletes?  I was thinking of just using

while(bo.count > 0)

I just wanted to point this out so that other FoxPro converts don't run into the same issue and not even realize it.

Scott
Scott
StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)
Group: Forum Members
Posts: 176, Visits: 1.5K
I quickly found that you need to abort that loop if a record is NOT delete, or you will get an infanet loop.

            bool answersDeleted = true;

            DCAnswerBO dcAnswer = new DCAnswerBO();

            dcAnswer.FillByQuestionID(vDCQuestID,"");

            if (dcAnswer.MoveFirst())

                do

                {

                    if (dcAnswer.DeleteCurrentRow() == 0)

                        answersDeleted = false;

                //} while (dcAnswer.Count > 0);

                } while (dcAnswer.Count > 0 && answersDeleted);

StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Yes, deleting items from within a collection can be quite tricky.  If you modify a collection while iterating through the collection using an IEnumerator (foreach loop), then you'll get an exception if you try to add or delete an item from the collection. 

Similar problems occur if you're going through a standard for loop, because the items you're iterating through change with respect to the indexer of the loop. 

The best way I have found is to do what you did... a while loop testing on the count of the records.  You could also change the answersDeleted variable from a Boolean to an integer and increment it for each record that is skipped.  Then just test to see if the count is greater than the number of records not deleted, meaning you still have more to check.

Scott
Scott
StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)StrataFrame User (270 reputation)
Group: Forum Members
Posts: 176, Visits: 1.5K
Good idea.  I like that.  Thanks for the confirmation.
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
No problem Smile
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search