StrataFrame Forum

BO filters

http://forum.strataframe.net/Topic11714.aspx

By Keith Chisarik - 9/26/2007

I am trying to use filters to update BO records that match certain criteria.



I took the code form the help file in which you update an active flag on a record. I am tryingto do much the same thing but it does not work as I expected.



When I first apply the filter, the record count is 5 (bo.count property is 5) and properly displays the correct records in CurrentView, as the code loops through it changes the count on the BO on the fly, decrementing by one. I did not expect this until I saved the changes after the loop, as a result only three records get the flag field changed (only three lines get written out to the console) and the BO.save call fails with an index exception.



Any thoughts?



Private Sub SortAndFilterSample()

'-- Sort the products by name and price

ProductsBO1.Sort = "prod_name, prod_price DESC"



'-- Only show active records

ProductsBO1.Filter = "prod_isactive=1"



'-- Iterate through all of the filtered rows and

' deactivate the products

If ProductsBO1.MoveFirst() Then



Do

'-- Write the product name to the Visual Studio console

Console.WriteLine(ProductsBO1.prod_name)



'-- De-activate

ProductsBO1.prod_isactive = False

Loop While ProductsBO1.MoveNext()



End If

End Sub
By Keith Chisarik - 9/26/2007

"Index 2 is either negative or above rows count." is what I get.
By Keith Chisarik - 9/26/2007

I got things working by using BO.copydatafrom to copy from the filtered BO to a new BO that contained only the filtered records, then I looped through the new BO, did my edits, and saved.



Doesn't seem to me that is how it should work, any comments appreciated. Perhaps I am confused Hehe


By Greg McGuffey - 9/26/2007

Ah, this looks like the old "I filtered my records, updated the field used to filter and got bit in the butt" trick! Blink



Since you are filtering on the prod_isactive field and then set that field to a new value, it will immediately filter that row out of the current view (if you set it to false that is). It will also immediately whack the current index (can't remember exactly, but it either leaves the index the same, but points to a new row or changes the index to -1 because after the filter there is no current record...its weird in any case). The copydatafrom is much safer, as the copied bo will then only have the records you need, no filter and no strangeness.
By Keith Chisarik - 9/26/2007

it is weird, one might go so far as to say its a dumb way to do it ??? Smile



I did find copydatafrom for use with a filter, thanks for letting me know that was the right choice and that the behavior is expected.


By Trent L. Taylor - 9/27/2007

Keith,

As you have already discovered, the reason you had the problem is because you were changing the values of the field on which the filter was applied while enumerating the collection.  This will get you everytime Smile  Another way to go is to create a List or array of the items you want to update while in this loop, then enumerate the list and apply the changes to the BO.  This way you will never have the collection you are enumerating change.  Just another thought Smile