Solved

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

Posted on 2011-09-03
7
4,261 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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