Which dataset row violates the only DB constraint?

I am writing an application using C # 2005 and Sql Server 2000.

I have a table with a unique constraint, in which case I have two users using a form that will (when clicking Save) update the table.

If the table is, say, NAMES (ID int, NAME varchar (20)) and the unique constraint is on NAME, if the first user to save adds the names NAME "David" and "John", then that's fine. If a second user tries to update using a DataTable that contains strings named "John" and "Susan" NAME, then a SqlException is thrown. However, the exception doesn't say anything which tells me which row in my DataTable is breaking the constraint.

Apart from getting the details of the unique constraint composition from the DB and then using that information to validate each row in my DataTable to make sure that row violates the constraint, is there a way to determine which row is corrupted?

+2


source to share


2 answers


You have to use DataTable.GetErrors to get an array of DataRows with errors.



For each DataRow in the array, you should check for DataRow.RowError and which columns are in error with DataRow.GetColumnsInError .

+2


source


It is also possible that the rows in the DataTable simply conflict with each other, and therefore the database operation fails, although nothing in the database transaction conflicts with the DataTable.



+1


source







All Articles