Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. find Duplicates in Datatable

find Duplicates in Datatable

Scheduled Pinned Locked Moved .NET (Core and Framework)
questiondatabase
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SamRST
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • S SamRST

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      My blog | My articles | MoXAML PowerToys | Onyx

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        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.

        My blog | My articles | MoXAML PowerToys | Onyx

        S Offline
        S Offline
        SamRST
        wrote on last edited by
        #3

        Even though v create a constrain, how v can check in database table to datatable datas

        R 1 Reply Last reply
        0
        • S SamRST

          Even though v create a constrain, how v can check in database table to datatable datas

          R Offline
          R Offline
          Rob Smiley
          wrote on last edited by
          #4

          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"

          P 1 Reply Last reply
          0
          • R Rob Smiley

            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"

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups