Record duplication

Closed
mikeoe2003 Posts 24 Registration date Thursday November 1, 2012 Status Member Last seen March 27, 2014 - Nov 1, 2012 at 05:11 AM
Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 - Nov 1, 2012 at 05:20 AM
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 responses

mikeoe2003 Posts 24 Registration date Thursday November 1, 2012 Status Member Last seen March 27, 2014 2
Nov 1, 2012 at 05:20 AM
sorry guy i have spotted my mistake thanks
0
Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Nov 1, 2012 at 05:20 AM
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.

0