Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-03
7
Medium Priority
?
5,751 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Accepted Solution

by:
disrupt earned 500 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 500 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Integration Management Part 2

886 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