Hello everybody.I am amking a programm in vb net and mysql.
My question is the following:
i have one main form which it loads ,it loads all the database data in a datagridview.When i press the button"add customer" it opens anew form where i enter the person details.Then when i click the save button on this form,it closes and the main form is again opened and here is the problem:my datagrid view it has instead of one record ,one more...for example if i already have 4 records and add one more that is five...te datagrid will show 10..

I have tried to put when the form2 button save is clicked : myDataGridView.DataSource = Nothing or myDataTable.Clear() or DataGridView.Rows.Clear()
but again the same problem.

Recommended Answers

All 7 Replies

Post some code and then we can really help.
Any the rows repeated as a group? I.e. you have rows 1,2,3,4,1,2,3,4?
Because that is a clear indicator your loading code is runnning twice.
Are you using the datasouce and databind methodss or adding the items to the grid row by row in code?
Anyway, a code sample will help.

So in the beginning when the form1 loads the sub datagridview_load() is used.As you can see later i explain this sub loads the data from my mysql database.So after that when i press an add button i have,a second form is opened "ΝΕΟΣ_ΑΣΘΕΝΗΣ".So far so good..after that when i fill all the info and press save,instead of insrting just the record i created ,it inserts this plus all the others i have already uploaded in the beginning in my datagridview when i first run the programm...Eg: i have in my database 2 records and i run the program.My datagriview will display correctly these two.After i add a third ,when i will return to form1,that is main screen,instead of 3 it will have 6!
Hope someone can help me....Thank in advance..

ps:Sorru for my code being in my language ,but i thought it would be better that way.Of course it is not all my programm ,i have uploaded the needed parts so that i will not confuse.!

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AMKA_combobox_load()
            name_combobox_load()

            '--------------updates the navigator when a user is added------'
            filldatasetandview()
            bindfields()
            showposition()
            '--------------------------------------------------------------'

            searchpreference_load()

            '-------------------Loads the data into the datagrid table-----'


            datagridview_load()
            '--------------------------------------------------------------'
        End Sub



    Public Sub datagridview_load() 'Loads the data into the datagrid table'

            mysqlconn = New MySqlConnection
            mysqlconn.ConnectionString = "server=localhost;userid=root;password='';database=doctorsuite"
            Dim sda As New MySqlDataAdapter

            Dim bsource As New BindingSource


            Try

                mysqlconn.Open()
                MessageBox.Show("ok")
                Dim query As String
                query = "select * from doctorsuite.patient "
                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 Exception
                MessageBox.Show(ex.Message)
            End Try


        End Sub


second form:
    Imports MySql.Data.MySqlClient
    Public Class ΝΕΟΣ_ΑΣΘΕΝΗΣ

        Dim mysqlconn As MySqlConnection
        Dim command As MySqlCommand

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            '-------------------insert a new patient to database-----------'
            add_new_patient()
            '--------------------------------------------------------------'

            '--------------updates the navigator when a user is added------'
            Form1.filldatasetandview()
            Form1.bindfields()
            Form1.showposition()
            '--------------------------------------------------------------'

            Form1.DataGridView1.Rows.Clear()

            Form1.Show()
        End Sub

        Public Sub add_new_patient() 'sub for adding a new record in database'
            mysqlconn = New MySqlConnection
            mysqlconn.ConnectionString = "server=localhost;userid=root;password='';database=doctorsuite"
            Dim sda As New MySqlDataAdapter
            Dim dbdataset As New DataTable
            Dim bsource As New BindingSource
            Dim reader As MySqlDataReader
            Try
                mysqlconn.Open()
                MessageBox.Show("ok")
                Dim query As String
                query = "insert into  doctorsuite.patient(p_id,name,surname,fathername,birthdate,address,town,postcode,country,tel1,tel2,mobile1,mobile2,fax1,fax2,sex,weight,height,BMI,email,tameio,iatros_parapempon,AMKA) values ('" & TextBox1.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & TextBox15.Text & "','" & TextBox16.Text & "','" & TextBox17.Text & "','" & TextBox18.Text & "','" & TextBox19.Text & "','" & TextBox20.Text & "','" & TextBox21.Text & "','" & TextBox25.Text & "','" & TextBox23.Text & "','" & TextBox24.Text & "','" & TextBox2.Text & "')"


                command = New MySqlCommand(query, mysqlconn)
                reader = command.ExecuteReader

                mysqlconn.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try


        End Sub








    End Class

So in the beginning when the form1 loads the sub datagridview_load() is used.As you can see later i explain this sub loads the data from my mysql database.So after that when i press an add button i have,a second form is opened "ΝΕΟΣ_ΑΣΘΕΝΗΣ".So far so good..after that when i fill all the info and press save,instead of insrting just the record i created ,it inserts this plus all the others i have already uploaded in the beginning in my datagridview when i first run the programm...Eg: i have in my database 2 records and i run the program.My datagriview will display correctly these two.After i add a third ,when i will return to form1,that is main screen,instead of 3 it will have 6!
Hope someone can help me....Thank in advance..

ps:Sorry for some of my code being in my language ,but i thought it would be better that way.Of course it is not all my programm ,i have uploaded the needed parts so that i will not confuse.!

Ps:i am Using aain the sub datavrIdview-load Because i want when returnIng to tHe main screen tje datagrid to be updated live,so i do not have to close thE forma nd reopen it.And yes,the duplicate entries are in datagrid and not in my sql table,beCise i use the aBove datagridview-load sub for tje reeason i explained.

Try adding the following: DataGridView1.DataSource = Nothing to datagridview_load

    Public Sub datagridview_load() 'Loads the data into the datagrid table'

           'prevents duplicates
           DataGridView1.DataSource = Nothing

           mysqlconn = New MySqlConnection
            mysqlconn.ConnectionString = "server=localhost;userid=root;password='';database=doctorsuite"
           Dim sda As New MySqlDataAdapter

          Dim bsource As New BindingSource

                ...
    End Sub

I noticed that you have the following in your code: Dim dbdataset As New DataTable. The name "dbdataset" is a little misleading, because you define it as a DataTable, not a DataSet.

It is better to create a Module to declare your connection string in, which all of the other forms can use. Then if any of the information changes, you only have to change it once.

Module1.vb

Module Module1
    Public connectStr = "server=localhost;userid=root;password='';database=doctorsuite"

End Module

You can also reset the DataTable, before filling it again:

Private dbDataTable As New DataTable

        ...

Private Sub datagridview_load()

    'prevent duplicates
    DataGridView1.DataSource = Nothing

    If dbDataTable.IsInitialized Then
        dbDataTable.Reset()
    End If

    Dim mysqlconn As New MySqlConnection
    mysqlconn.ConnectionString = connectStr

    Dim sda As New MySqlDataAdapter

    Dim bsource As New BindingSource

        ...

    Try

        ...

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

Additionally, it is best to use parameterized queries. See this post for more info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.