find Duplicates in Datatable
-
hi, i had filled my datatable with datas from Excel file. i want to insert these datas to a database table, before insertion i want to check is there any duplicate document_number in datatable. how can i check this without looping each rows in datable? Advance thanks...Sam.
-
hi, i had filled my datatable with datas from Excel file. i want to insert these datas to a database table, before insertion i want to check is there any duplicate document_number in datatable. how can i check this without looping each rows in datable? Advance thanks...Sam.
You could always set up a UniqueConstraint on your datatable to detect if you have any constraint violations. It would look something like this:
DataColumn col = myDataTable.Columns[0];
UniqueConstraint constraint = new UniqueConstraint(col);
myDataTable.Constraints.Add(constraint);"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You could always set up a UniqueConstraint on your datatable to detect if you have any constraint violations. It would look something like this:
DataColumn col = myDataTable.Columns[0];
UniqueConstraint constraint = new UniqueConstraint(col);
myDataTable.Constraints.Add(constraint);"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Using a constraint as suggested won't tell you which rows are duplicated. To find this programatically, you have to loop through the rows at least once. Try creating a DataView of the table and sorting it by the column that contains duplicates. Then loop through the rows in the view & check each value against the previous row value
"An eye for an eye only ends up making the whole world blind"
-
Using a constraint as suggested won't tell you which rows are duplicated. To find this programatically, you have to loop through the rows at least once. Try creating a DataView of the table and sorting it by the column that contains duplicates. Then loop through the rows in the view & check each value against the previous row value
"An eye for an eye only ends up making the whole world blind"
Rob - UniqueConstraints throw a ConstraintException when the constraint is broken. This provides more than enough info to work out what's been violated.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.