Dynamic Editable Collection - how to prevent duplicate root objects from being created?

Dynamic Editable Collection - how to prevent duplicate root objects from being created?

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

xvblack posted on Tuesday, August 17, 2010


I've been using CSLA .NET for some time now and I've just come across the need to use a Dynamic Root Collection bound to a datagridview control.

I've got it all working apart from one thing that I'm at a loss to resolve. When a User adds a new row to the datagrid how can you check if the entry they're adding already exists? For example, the grid shows a collection of Dynamic Root objects that have code and desciption properties. The code property is actually databound to the primary key of an underlying database table and therefore cannot be duplicated.

If the User tries to add a key that already exists the Data Portal throws an exception saying you cannot add a duplicate primary key (which is expected). Is there anyway to trap this error before it gets to the Data Portal? The root object has an 'exists' method but the question is how do I trap the error in the first place?

Any help would be much appreaciated.




Jav replied on Tuesday, August 17, 2010

You can create a BusinessRule (or ValidationRule, depemding upon the Csla version you are using) for your code property.  The rule (which will run as soon as the user tabs out of the code field) should check the entered code against the other items in the DataGrid to see if there is a duplication.  If so, it will register as a broken rule and the user will get am immediate warning in the UI.

The above assumes that all your existing data is already loaded in the DataGrid (which would be rare).  Normally, you will have to write code in your BusinessRule to check for this duplication in the database also.  I believe some of Rocky's sample projects do demonstrate that.


xvblack replied on Wednesday, August 18, 2010

Thanks for the quick response Jav. I've looked at Rocky's examples and have found the relevant bit of code.

It all works fine now!



Copyright (c) Marimer LLC