Solved

How to check if duplicated rows exist in a vb.net datatable

Posted on 2011-09-03
7
5,021 Views
Last Modified: 2012-05-12
I have a datatable defined with a few columns
One of the columns is Code, How can I check that the datatable does not have duolicate values in the code column
0
Comment
Question by:countrymeister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:Ambusy
ID: 36478245
SELECT code, COUNT( * ) AS cnt
FROM  `table`
GROUP BY code
HAVING COUNT( * ) >1
0
 
LVL 4

Expert Comment

by:Ambusy
ID: 36478251
If you want to avoid duplicates appearing in the database, define a UNIQUE key on the column(s)
0
 
LVL 1

Author Comment

by:countrymeister
ID: 36478359
we are discussing a vb.net datatable not a sql datatable
I did find a solution though
'Loop for all the rows  datatable
        For Each dtRow As DataRow In dTable.Rows
            'Check is there any row having same record
            Dim dtSearchedRows As DataRow() = dTable.Select("ProductId='" & dtRow("ProductId") & "' and BrochureId='" & dtRow("ProductId") & "'")
            'If there is any duplicate row
            If dtSearchedRows.Length > 0 Then
'throw erorr duplicate exist
                      End If
        Next
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 16

Accepted Solution

by:
disrupt earned 125 total points
ID: 36478782
Using LINQ:

var duplicates = d.AsEnumerable().GroupBy(i => i.Field<string>("COL NAME HERE")).Where(g => g.Count() > 1).Select(g => g.Key);

foreach (var dup in duplicates) {

}
0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 125 total points
ID: 36479488
You can use List (or other enumerable like HashTable or Dictionary) as well:
Dim lstUnique as New List(Of String)
Dim lstDuplictes as New List(Of String)
Dim strCheck As String
For Each dtRow As DataRow In dTable.Rows
       strCheck = dtRow("ProductId") & "_" & dtRow("BrochureId")
       If lstUnique.Contains(strCheck) Then
           'Dupe exists
          lstDuplicates.Add(strCheck)  
       Else
          lstUnique.Add(strCheck)
       End If
Next

Open in new window

0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 36480513
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 36480571
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question