Solved

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

Posted on 2011-09-03
7
4,436 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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