How to fix double output on DataGridView on VB.Net

I am using MySQL on the back end. My program has two datagridviews, one for the employee table and the other for the employee logs. The problem is whenever I load the data onto the two datagridviews, the data is duplicated. For example there are 4 data on the employee table, it will display the 4 data and then it will display another set of that data just below the first 4. The two datagridviews does that. What seems to be the problem in my code?

Imports MySql.Data.MySqlClient
Public Class ViewMealLog


Dim dbDataSet As New DataTable
Dim dbDataSet2 As New DataTable

Public sConnection As New MySqlConnection

Private Sub load_table()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource


    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub

Private Sub load_table2()
    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDAX As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource


    Try
        sConnection.Open()
        Dim Query As String
        Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDAX.SelectCommand = sqlCommand
        SDAX.Fill(dbDataSet2)
        bSource.DataSource = dbDataSet2
        DataGridView2.DataSource = bSource
        SDAX.Update(dbDataSet2)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    load_table2()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDAX As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource
    Try
        sConnection.Open()
        Dim Query As String
        Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDAX.SelectCommand = sqlCommand
        SDAX.Fill(dbDataSet2)
        bSource.DataSource = dbDataSet2
        DataGridView2.DataSource = bSource
        SDAX.Update(dbDataSet2)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    load_table()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource
    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub


Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    MainMenu.Show()
    Me.Hide()
End Sub


Private Sub txtSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch.TextChanged
    Dim DV As New DataView(dbDataSet)
    DV.RowFilter = "CONVERT(emp_no, System.String) LIKE '%" & txtSearch.Text & "%' "
    DataGridView1.DataSource = DV
End Sub

Private Sub txtSearch2_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch2.TextChanged
    Dim DV As New DataView(dbDataSet)
    DV.RowFilter = "CONVERT(log_date, System.String) LIKE '%" & txtSearch2.Text & "%' "
    DataGridView1.DataSource = DV
End Sub



Private Sub txtSearch3_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch3.TextChanged
    Dim DV As New DataView(dbDataSet2)
    DV.RowFilter = "CONVERT(emp_no, System.String) LIKE '%" & txtSearch3.Text & "%' "
    DataGridView2.DataSource = DV
End Sub

Private Sub txtSearch4_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch4.TextChanged

    Dim DV As New DataView(dbDataSet2)
    DV.RowFilter = "CONVERT(emp_lastnm, System.String) LIKE '%" & txtSearch4.Text & "%' "
    DataGridView2.DataSource = DV
End Sub
End Class

Why would you call the load_table and load_table2 function in your button click event? try removing the function call as your button click event already query the database.

 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
"'load_table2()<<-----"

     sConnection = New MySqlConnection
     sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

     Dim SDAX As New MySqlDataAdapter
     Dim sqlCommand As New MySqlCommand
     Dim bSource As New BindingSource
     Try
         sConnection.Open()
         Dim Query As String
         Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
         sqlCommand = New MySqlCommand(Query, sConnection)

         SDAX.SelectCommand = sqlCommand
         SDAX.Fill(dbDataSet2)
         bSource.DataSource = dbDataSet2
         DataGridView2.DataSource = bSource
         SDAX.Update(dbDataSet2)

         sConnection.Close()

     Catch ex As MySqlException
         MessageBox.Show(ex.Message)
     Finally
         sConnection.Dispose()
     End Try
 End Sub

UPDATE

OR like this

 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
      load_table2()    
 End Sub

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image