StrataFrame Forum

Save after edit gives broken rules on unchanged text

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

By Andria Jensen - 6/22/2006

I have a user control which is basically like the maintenance tool strip.  It has save, new, delete, edit, and undo buttons which function on a business object and linked to controls on the form.  I have some business rules setup which prevent duplicate names from being entered into the database.  This works perfectly when doing a New operation and saving.  However, if I edit a record and then try to save it will give me a broken rule for things that haven't changed.  For example, I have a FirstName and a LastName field.  I want to only edit the FirstName, but leave the LastName alone.  If I do this, and then save it will give me a broken rule on LastName if I'm not allowing duplicates.  Is there some way around this?  Maybe a setting that tells the BO not to check rules on fields that haven't changed from their original state?
By Trent L. Taylor - 6/22/2006

Andria,

Your business rule needs to exclude the record your are working on.  The best way to do this is include the primary key as part of the test.  This will work for new or existing records.

Example:

'-- Establish Locals
Dim loCommand As New SqlCommand()

'-- Build the command
loCommand.CommandText = "SELECT COUNT(*) FROM MyTable WHERE mt_pk != @mt_pk AND mt_lastname = @mt_lastname"

'-- Add the parms
loCommand.Parameters.Add("@mt_pk", Data.SqlDbType.Int)
loCommand.Parameters.Add("@mt_lastname", Data.SqlDbType.VarChar)

'-- Set the parms
loCommand.Parameters("@mt_pk").Value = PassedPrimaryKey
loCommand.Parameters("@mt_lastname").Value = PassedLastName

'-- Return Boolean Results
Return CType(Me.ExecuteScalar(loCommand), Integer) > 0

By Andria Jensen - 6/22/2006

Thanks, that works great!
By Trent L. Taylor - 6/22/2006

Good.  Glad to help Smile