Unique Constraint Validation in SS

Posts   
 
    
jspanocsi
User
Posts: 145
Joined: 04-Mar-2005
# Posted on: 18-Mar-2005 14:30:06   

What's the best way to do unique constraint validation in the Self Servicing model?

For instance, if I have a table that's recordid, userid, username in sql server Recordid is a identity, unique constraint is on userid. If I try to save an entity back to the db that violates the constraint I get an exception thrown by llbl that show the violation of the constraint from the db. Is there a way to check on this ahead of time in the entity validator or something similar so I can handle it with out round tripping to the db?

This would also let me show an error provider next to a row in a grid like I do for all my other validations. I had my entities implement IDataErrorInfo. Since the constraint just fires an exception, the error providers aren't show.

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Mar-2005 18:01:03   

You always need to consult the database for this, as some other user could have inserted a row so the code has to check if the database has already that value. You could try to run a scalar query (using GetScalar on adapter or entityCollection in selfservicing) and check if the value already exists. If not, proceed. This isn't totally fail safe, but could work in most situations. The only real safe way is by proceeding with teh insert and catch the exception.

Frans Bouma | Lead developer LLBLGen Pro