First of all, you probably want to enforce the UNIQUE constraint in the database itself. As far as handling the validation, you can either trap the SQL Exception and parse (though this approach is probably not very portable and somewhat brittle). Or you can do a select that excludes the current record.

SELECT FROM foo WHERE foo.id != myFoo.id and foo.uniqueCode = myFoo.uniqueCode;

if this select returns one or more records then you know that the selected Code on the myFoo object is not valid.

Paul Barry wrote:

When I am writing a form that updates a record in the database, the best practice is to pre-populate the form with the data from the database and then just update all of those values in the database, regardless of whether or not they have changed, right? So what happens when you have a field that is required to be unique throughout the application, like a code or something? I assume that before updating that record you need to do a select, to see if there already is another record in the database with that value, right? So when the user changes that value to something that is already being used by another record, you can throw an exception like "code already in use". But what happens if the user didn't modify that unique field? The select will return a record, so the logic will already think there is already a user with that record.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to