Solved

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

Posted on 2011-09-03
7
3,929 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 27

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now