Reputation: 1
Can you guys help me?
Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
If BtnSimpan.Text = "&Simpan" Then
If txtKode.Text = "" Then
MsgBox("Kode Harus Di isi", MsgBoxStyle.Exclamation, "Peringatan")
txtKode.Focus()
Exit Sub
End If
Call bukaDB()
CMD = New MySqlCommand("SELECT KodeBarang from tabelbarang WHERE KodeBarang = '" & txtKode.Text & "'", Conn)
RD = CMD.ExecuteReader()
RD.Read()
If RD.HasRows Then
MsgBox("Maaf, Data dengan Kode tersebut telah ada", MsgBoxStyle.Exclamation, "Peringatan")
Else
simpan = "INSERT INTO tabelbarang (KodeBarang,NamaBarang,HargaBeli,HargaJual,Stok) VALUES ('" & txtKode.Text & "','" & txtNamaBarang.Text & "','" & txtHargaBeli.Text & "','" & txtHargaJual.Text & "','" & txtStok.Text & "')"
CMD = New MySqlCommand(simpan, Conn)
CMD.ExecuteNonQuery()
Call isiGrid()
BtnSimpan.Text = "&Tambah"
Call Bersih()
End If
Else
BtnSimpan.Text = "&Simpan"
'Call Bersih()
txtKode.Enabled = True
txtNamaBarang.Enabled = True
txtHargaBeli.Enabled = True
txtHargaJual.Enabled = True
txtStok.Enabled = True
txtKode.Focus()
End If
End Sub
Upvotes: 0
Views: 185
Reputation: 416091
There were several out-dated practices in the code from the question. The code below is updated for modern coding styles, and the mere act of using modern styles will also solve the issue from the question... that is, if you had followed good coding practices from the beginning, this whole class of issue is solved and you would never have had this problem.
This also fixes the HUGE GAPING SECURITY ISSUE from the original, which again would have been avoided completely just from keeping up with modern coding standards.
Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
If BtnSimpan.Text = "&Simpan" AndAlso String.IsNullOrWhitespace(txtKode.Text) Then
MsgBox("Kode Harus Di isi", MsgBoxStyle.Exclamation, "Peringatan")
txtKode.Focus()
Exit Sub
End If
If BtnSimpan.Text <> "&Simpan" Then
BtnSimpan.Text = "&Simpan"
'Bersih()
txtKode.Enabled = True
txtNamaBarang.Enabled = True
txtHargaBeli.Enabled = True
txtHargaJual.Enabled = True
txtStok.Enabled = True
txtKode.Focus()
Exit Sub
End If
'Do NOT try to re-use the same connection throughout your application!
' It really is more efficient to create a brand new object for most queries,
' and only share the connection string.
'Also, JUST DO THE INSERT.
'Make sure there is a unique constraint on the KodeBarang column,
' and handle the exception if it fails.
' Correct for *either one* of the above issues, and the
' problem in the question never would have happened.
Try
Using CN As New MySqlConnection("Connection string here"), _
CMD As New MySqlCommand("INSERT INTO tabelbarang (KodeBarang,NamaBarang,HargaBeli,HargaJual,Stok) VALUES (@KodeBarang, @NamaBarang, @HargaBeli, @HargaJual, @Stok)", CN)
CMD.Parameters.AddWithValue("@KodeBarang", txtKode.Text)
CMD.Parameters.AddWithValue("@NamaBarang", txtNamaBarang.Text)
CMD.Parameters.AddWithValue("@HargaBeli", txtHargaBeli.Text)
CMD.Parameters.AddWithValue("@HargaJual", txtHargaJual.Text)
CMD.Parameters.AddWithValue("@Stok", txtStok.Text)
CN.Open()
CMD.ExecuteNonQuery()
End Using
isiGrid()
BtnSimpan.Text = "&Tambah"
Catch ex As MySqlException When ex.Code = 1062 '1062 is Duplicate Key Violation
MsgBox("Maaf, Data dengan Kode tersebut telah ada", MsgBoxStyle.Exclamation, "Peringatan")
End Try
End Sub
Upvotes: 1