Record duplication

[Closed]
Report
Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
-
Posts
2368
Registration date
Sunday September 23, 2012
Status
Member
Last seen
December 13, 2018
-
Hello,
Please each time i click the botton (btn1) the record is added twice in my table.

Imports System.Data.OleDb
Public Class Form1

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Try
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim mySQLString As String
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\prog3\insert_form\Database5.mdb;")
myConnection.Open()
mySQLString = mySQLString = "INSERT INTO myuser (username, passwords) VALUES('" & txtid.Text & "','" & txtpw.Text & "')"
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
MessageBox.Show("Record Added Successfuly")
txtid.Text = ""
txtpw.Text = ""
End Sub
End Class


2 replies

Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
2
sorry guy i have spotted my mistake thanks
Posts
2368
Registration date
Sunday September 23, 2012
Status
Member
Last seen
December 13, 2018
46
Hi mikeoe2003,

If you notice, you have two instances of:

myCommand.ExecuteNonQuery()

Remove the last instance and the data will be entered only once.

Your original code:

myCommand.ExecuteNonQuery()
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
Catch ex As Exception

Replace the above with:

myCommand.ExecuteNonQuery()
myCommand = New OleDbCommand(mySQLString, myConnection)
Catch ex As Exception

Please reply if you have any further questions.