I've got a simple mdb database set up in Visual Basic Express Edition (winforms) and I'm trying to update data from a datagridview to a database. I've databinded the textboxes to the columns I want in the datagridview and this works great.
我在Visual Basic Express Edition(winforms)中设置了一个简单的mdb数据库,我正在尝试将数据从datagridview更新到数据库。我已经将文本框数据化为datagridview中我想要的列,这非常有用。
After some struggling, I've finally managed to have the values updated in every row of the database. But, trying to modify a newly created record throws this error: "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records"
经过一番努力,我终于设法在数据库的每一行都更新了值。但是,尝试修改新创建的记录会抛出此错误:“并发冲突:UpdateCommand影响了预期的1条记录中的0条”
I mean, it seems as though the error only happens with newly created rows. Because closing the application and reopening it immediatly, I'm allowed to update the values of the cells. I also noticed that the ID of the row is set to "-1" when I've just created a new row. The other rows don't have a negative number for ID. Could this be the cause? Seems as though the row isn't really updated to the database.
我的意思是,似乎错误只发生在新创建的行上。因为关闭应用程序并立即重新打开它,我可以更新单元格的值。我还注意到,当我刚创建一个新行时,该行的ID设置为“-1”。其他行没有ID的负数。这可能是原因吗?好像该行没有真正更新到数据库。
So this is the code for the AddRow button
所以这是AddRow按钮的代码
Dim newRow As MusicDBDataSet.SongsRow
newRow = MusicDBDataSet.Songs.NewSongsRow()
newRow.Name = txtBoxNewName.Text
newRow.Genre = txtBoxNewGenre.Text
newRow.Rhytm = txtBoxNewRhytm.Text
newRow.Length = txtBoxNewLength.Text
MusicDBDataSet.Songs.Rows.Add(newRow)
Try
Me.Validate()
Me.SongsTableAdapter.Update(Me.MusicDBDataSet.Songs)
Me.SongsBindingSource.EndEdit()
Me.MusicDBDataSet.Songs.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
This fills the data I've put into the text boxes into a new row of the DataGridView. Everything looks fine (except for the -1 value of the ID column)
这将我放入文本框的数据填充到DataGridView的新行中。一切都很好(除了ID列的-1值)
The textboxes that are databinded to the database, these are the ones I'm using to try to update the values of the cells. And the code looks like this:
数据绑定到数据库的文本框,这些是我用来尝试更新单元格值的文本框。代码看起来像这样:
Try
Me.Validate()
Me.SongsBindingSource.EndEdit()
Me.SongsTableAdapter.Update(Me.MusicDBDataSet.Songs)
Me.MusicDBDataSet.Songs.AcceptChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now, I'm not sure at all if this is the correct way of doing this. But thinking about it, the textboxes used to update the values of the newly created row are databinded to the database or tableadapter itself right? Could it be that the UpdateCommand fails because the row hasn't really been created properly yet?
现在,我完全不确定这是否是正确的做法。但考虑到这一点,用于更新新创建的行的值的文本框是数据绑定到数据库或tableadapter本身吗?可能是UpdateCommand失败了,因为还没有真正创建行吗?
2 个解决方案
#1
3
I resolved it as follows:
我解决了如下:
After modifying data table do following step.
修改数据表后执行以下步骤。
dataTable = dataTable.GetChanges()
#2
-1
The easiest way is to open the dataset in designer. Delete the update, insert and delete commands, choose table adapter - configure - advanced options - generate insert, update and delete statements. Leave the second option unchecked.
最简单的方法是在设计器中打开数据集。删除更新,插入和删除命令,选择表适配器 - 配置 - 高级选项 - 生成插入,更新和删除语句。不选中第二个选项。
#1
3
I resolved it as follows:
我解决了如下:
After modifying data table do following step.
修改数据表后执行以下步骤。
dataTable = dataTable.GetChanges()
#2
-1
The easiest way is to open the dataset in designer. Delete the update, insert and delete commands, choose table adapter - configure - advanced options - generate insert, update and delete statements. Leave the second option unchecked.
最简单的方法是在设计器中打开数据集。删除更新,插入和删除命令,选择表适配器 - 配置 - 高级选项 - 生成插入,更新和删除语句。不选中第二个选项。