-1

I want to make show only the duplicated rows, and delete all the single rows ( non-dublicated ) in the GridView

My Code Is :

 Private Sub load_table()
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=nassa640088;database=testing"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select * from testing.login"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        SDA.SelectCommand = COMMAND
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)



        MysqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try

End Sub
Ahmed Salem
  • 55
  • 1
  • 9
  • Do this in your query rather than after. Have a look at [this](http://stackoverflow.com/questions/4401326/sql-return-only-duplicate-rows) – Bugs Oct 21 '16 at 12:35
  • Cannot do that from the VB.NET ? – Ahmed Salem Oct 21 '16 at 12:53
  • I see no reason why you would do that. You're making more work for yourself. If you did that you would have to bring into a collection and then work out which ones are duplicated. The answer below has the query for you to work with. Change `select * from testing.login` to the answer below changing the column names as required. – Bugs Oct 21 '16 at 12:55
  • If you did need to do it afterwards look into Linq and `Group By`. Have a look at [this](http://stackoverflow.com/questions/7732132/linq-vb-how-to-check-for-duplicates-in-a-list-of-objects) which may help. – Bugs Oct 21 '16 at 13:00
  • Aha thanks that helped – Ahmed Salem Oct 21 '16 at 13:01

1 Answers1

1

In that case change your query using group by like (assuming there is an id column)

select * from testing.login where id in (
select id 
from testing.login
group by id
having count(id) > 1 );
Rahul
  • 76,197
  • 13
  • 71
  • 125