Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove dublicate records from a datatable

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello people,
I have got a datatable.

How do I delete dublicate records?

Thank you in advance

 
What sort of database?
Can you provide the table structure?
 
here you go:

Code:
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim da As SqlDataAdapter
        Dim SQLStr As String
        Dim cnn As SqlConnection
        DataGridView1.DataSource = Nothing
        cnn = New SqlConnection(connectionString)
        cnn.Open()

        'Query för alla kolumner
        SQLStr = "SELECT " & _
                 "CAST (tt.OrderNr AS varchar(10)) + ' – ' + PostIt.Text AS 'New Column' " & _
              "FROM [myDB].dbo.OpusOrder as tt INNER JOIN" & _
              "[myDB].dbo.CompanyMain as c On tt.bolagsnr = c.id INNER JOIN" & _
              "[myDB].dbo.OpusOrderrow as ord On ord.ordernr = tt.ordernr INNER JOIN" & _
              "[myDB].dbo.PostIt as PostIt On PostIt.ordernr = tt.ordernr INNER JOIN" & _
              "[myDB].dbo.OrderCompanyLanguageName as snSrc ON ord.kallspraknr = snSrc.spraknr INNER JOIN" & _
              "[myDB].dbo.OrderCompanyLanguageName as snTrg ON ord.malspraknr = snTrg.spraknr"
        da = New SqlDataAdapter(SQLStr, TTCon)
     
                Dim myDatatable As New DataTable()
        da.Fill(myDatatable)
        For Each row As DataRow In myDatatable.Rows
            If Not row.Item(0).ToString().Contains("Language:") Then
                row.Delete()
            End If
        Next
        myDatatable.Columns(0).ColumnName = "Context Matches"


' I BELIEVE IT MUST BE HERE I MADE THE FUNCTION.
        DataGridView2.DataSource = myDatatable
      
    End Sub
 
What we need is the list of columns in the table in which you have duplicates. Which column (if any) is the primary key. I'm guessing that you are using SQL Server.
 
well I only have got one column and its called "Context Matches" I hope otherwise its just one column so (0)
 
So "Context Matches" is the column which contains duplicate entries?

Where does its data come from?
 
is this what you mean?

Code:
   da = New SqlDataAdapter(SQLStr, TTCon)
Dim myDatatable As New DataTable()
da.Fill(myDatatable)
 
If you are referring to the query in your first post above, it doesn't contain a column called "Context Matches
 
yes of course.

I actually added SELECT DISTINCT seems to work so far. but always good to know how to make it anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top