I have a form with a data grid on it, theres 2 text boxes the user enters there username and password and it goes to the database that works fine..but ive got the code where it Updates the Datagrid straight away but it duplicates all the other data in the database aswell..Sorry if its confusing heres my code for INSERT

Try
            CommandInsert.Parameters.Add(New OleDbParameter("Username", txtUsername.Text))
            CommandInsert.Parameters.Add(New OleDbParameter("Password", txtPassword.Text))
            CommandInsert.ExecuteNonQuery()

            If MsgBox("Data Added") Then
                loaddata()
                
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

LoadData() is the code that Updates the Database with the new information

Public Sub loaddata()
        Try
            connection.Open()
        Catch ex As Exception
            MsgBox(" " & ex.Message)
        End Try
        adapter.Fill(datatable)
        datagrid.DataSource = datatable
    End Sub

As i said it duplicates everything in the table plus the new information.. i Just want the new information to be added.. Thank You for your time :)

Recommended Answers

All 6 Replies

Add dataGridView1.DataSource statement inside the Tick event of Timer.

On your data adapter check for a property named "ClearBeforeFill" -- I believe it is a designer generated property for typed table adapters. Another solution is to clear the database before you refresh:

Public Sub loaddata()
        Try
            connection.Open()
        Catch ex As Exception
            MsgBox(" " & ex.Message)
        End Try
        datatable.Clear()
        adapter.Fill(datatable)
        datagrid.DataSource = datatable
    End Sub

Thanks both for replys! I tried Sknake's Option and it does indeed work thanks..but there is another problem lol The data from the textbox does go into the database..but if i type the new data to add to the database the old data gets added again ill try and make more sense say..

The user enters there username "User1" and Password "123" the presses enter..it gets added to the database thats fine.. but when the user types in diffrent information.."User1" and Password "123" get added to the database again

You haven't posted enough information to indicate what might be causing this. Please post the code for your entire class

First, in order to solve the problem of the dublicate should delete all data that in dataGridViwe after that reload all your data that in the data base to te DataGridViwe

add clear textBox after use the textBox,that mean add
txtUserName.text=""
txtPassword.text=""
after your insert code

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.