views:

2224

answers:

0

Hi, 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.

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"

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.

So this is the code for the AddRow button

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)

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?