我需要帮助来更新我的数据网格上的数据。当我更改“ Kode”的值并单击“编辑”按钮时,它运行良好,但是当我更改“ Kode_Kategori_Kamar”或“状态”的值时,出现错误消息:
“并发违反update命令影响了预期的1条记录中的0条。”
我正在使用Visual Studio 2017和XAMPP(MySQL)。我尝试重新排列代码,删除WHERE子句,并使用try...catch
。
Imports MySql.Data.MySqlClient
Public Class frmKamar
Public hostconn As New MySqlConnection
Sub enable()
txtKode.Enabled = True
cmbKategori.Enabled = True
cmbStatus.Enabled = True
End Sub
Sub disable()
txtKode.Enabled = False
cmbKategori.Enabled = False
cmbStatus.Enabled = False
End Sub
Sub bersih()
txtKode.Clear()
cmbKategori.SelectedItem = Nothing
cmbStatus.SelectedItem = Nothing
End Sub
Public Sub frmKamar_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
frmMenu.Show()
dst.Tables.Clear()
conn.Close()
End Sub
Private Sub frmKamar_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If (ConnectionState.Closed) Then
conn.Open()
End If
End Sub
Private Sub frmKamar_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connect("localhost", "root", "", "dbhotel")
Try
da = New MySqlDataAdapter("select * from kamar", conn)
da.Fill(dst, "kamar")
dgKamar.DataSource = dst.Tables("kamar")
dgKamar.CaptionText = "Data ke. " & dgKamar.CurrentRowIndex + 1 & " of " & dst.Tables("kamar").Rows.Count()
Catch myerror As MySqlException
MessageBox.Show("Error connecting to Database : " & myerror.Message)
End Try
cmbStatus.Items.Add("Terisi")
cmbStatus.Items.Add("Kosong")
combobx()
disable()
End Sub
Sub combobx()
Try
Dim cd = New MySqlCommand("select * from kategori_kamar", conn)
rd = cd.ExecuteReader()
While rd.Read()
Me.cmbKategori.Items.Add(rd("kode"))
End While
rd.close()
Catch ex As Exception
MessageBox.Show("Koneksi Gagal ! : " & ex.Message)
End Try
End Sub
Dim state As StateEnum
Private Enum StateEnum
StateIdle = 0
StateNew = 1
StateEdit = 2
End Enum
Private Sub btnBaru_Click(sender As Object, e As EventArgs) Handles btnBaru.Click
enable()
state = StateEnum.StateNew
End Sub
Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
Select Case state
Case Is = StateEnum.StateNew
Dim databaru As DataRow = dst.Tables("kamar").NewRow
Try
databaru("kode") = txtKode.Text
databaru("kode_kategori_kamar") = cmbKategori.SelectedItem
databaru("status") = cmbStatus.SelectedItem
dst.Tables("kamar").Rows.Add(databaru)
MsgBox("Data berhasil ditambahkan", MsgBoxStyle.Information)
bersih()
Catch err As Exception
MsgBox(err.Message, MsgBoxStyle.Exclamation, "error")
dst = New DataSet
da = New MySqlDataAdapter("select * from kamar", conn)
da.Fill(dst, "kamar")
dgKamar.DataSource = dst.Tables("kamar")
Exit Sub
End Try
Case Is = StateEnum.StateEdit
Dim myEditRow As DataRow = dst.Tables("kamar").Rows(dgKamar.CurrentRowIndex)
Try
myEditRow("kode") = txtKode.Text
myEditRow("kode_kategori_kamar") = cmbKategori.SelectedItem
myEditRow("status") = cmbStatus.SelectedItem
Dim Sql As String
Sql = "UPDATE kamar SET kode = '" & txtKode.Text & "', kode_kategori_kamar = '" & cmbKategori.SelectedItem &
"', status = '" & cmbStatus.SelectedItem & "' where kode = '" & txtKode.Text & "'"
Dim cmd = New MySqlCommand(Sql, conn)
cmd.ExecuteNonQuery()
myEditRow.BeginEdit()
MsgBox("Data berhasil diupdate", MsgBoxStyle.Information)
dgKamar.Enabled = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "error")
dst = New DataSet
da = New MySqlDataAdapter("select * from kamar", conn)
da.Fill(dst, "kamar")
dgKamar.DataSource = dst.Tables("kamar")
Exit Sub
End Try
End Select
Dim datacomm As New MySqlCommandBuilder(da)
da.Update(dst, "kamar")
bersih()
disable()
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
state = StateEnum.StateEdit
Me.BindingContext(dst.Tables("kamar")).Position = dgKamar.CurrentRowIndex()
txtKode.DataBindings.Add("text", dst.Tables("kamar"), "kode")
cmbKategori.DataBindings.Add("text", dst.Tables("kamar"), "kode_kategori_kamar")
cmbStatus.DataBindings.Add("text", dst.Tables("kamar"), "status")
Dim ctrl As Control
For Each ctrl In GroupBox1.Controls
ctrl.DataBindings.Clear()
Next ctrl
dgKamar.Enabled = False
enable()
End Sub
Private Sub btnHapus_Click(sender As Object, e As EventArgs) Handles btnHapus.Click
dst.Tables("kamar").Rows(dgKamar.CurrentRowIndex).Delete()
Dim datacomm As New MySqlCommandBuilder(da)
da.Update(dst, "kamar")
End Sub
Private Sub btnBatal_Click(sender As Object, e As EventArgs) Handles btnBatal.Click
dgKamar.Enabled = True
bersih()
disable()
End Sub
Private Sub btnKeluar_Click(sender As Object, e As EventArgs) Handles btnKeluar.Click
Close()
End Sub
End Class