Concurrency Violation: the Update Command affected 0 of the expected 1 records

Laura Anderson 1 Reputation point
2022-12-12T11:43:41.193+00:00

I'm hoping someone can assist, I have an ASP.Net project, which has started throwing up a concurrency violation and I cannot find anyway of fixing it!

When saving new rows it works absolutely fine, it's when an existing row is amended it throws the error. The code below is where it is happening.

Friend Sub UpdatePlanner(ByRef DisplaySaveCompleteMessage As Boolean, ByRef WriteSequenceToTable As Boolean)

    If WriteSequenceToTable Then Seqence()  

    Dim ChangesTable As DataTable = dtPlannedItems2.GetChanges  

    If Not ChangesTable Is Nothing Then  
        Try  
            builder.GetUpdateCommand()  
            BoundadptSQL.Update(ChangesTable)  
            If DisplaySaveCompleteMessage Then  
                Me.CloseProcessing()  
                System.Threading.Thread.Sleep(200)  
                MsgBox("Planned data saved successfully.")  
            End If  

        Catch ex As Exception  
            Me.CloseProcessing()  
            System.Threading.Thread.Sleep(200)  

            MsgBox("There was an error saving the plan data!" & vbCrLf & ex.Message & vbCrLf & Err.Description, MsgBoxStyle.Exclamation)  
        End Try  
        dtPlannedItems2.AcceptChanges()  
    End If  
    Me.CloseProcessing()  
End Sub  

Any support would be greatly received.

Developer technologies | ASP.NET | Other
SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2022-12-12T15:51:56.47+00:00

    An update statement generally has a where clause. The error message means no rows matching were found. The code that builds the update statement has a bug.

    The other possibility is you are using optimistic concurrency and the time stamp is out of date.

    Use sql profile to debug.

    0 comments No comments

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-12-13T07:06:37.513+00:00

    Hi @Laura Anderson ,
    A concurrency error occurs when the data you're trying to update has already been updated between the time you got the data you were changing and the time you're trying to execute your update.
    It is recommended that you step through to see which step in your code is causing this data problem.

    If you're using optimistic concurrency, there are two general ways to determine if a change has occurred: the version method (actual version number or datetime stamp) and the method of saving all values.
    You can check out the documentation below.
    https://learn.microsoft.com/en-us/previous-versions/cs6hb8k4(v=vs.100)?redirectedfrom=MSDN#concurrency-control-in-adonet-and-visual-studio
    You can try the method provided in this thread:

    Me.yourTableAdapter.Update(Me.yourDataSet.yourTable)  
        Me.yourDataSet.youTable.AcceptChanges()  
        Me.yourTableAdapter.Fill(Me.yourDataSet.yourTable)  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.