Results 1 to 12 of 12

Thread: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

  1. #1
    alim's Avatar
    alim is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    bangladesh
    Posts
    89
    Reputation
    110

    Unhappy Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

    Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.



    i have seen this error many time and found some cure but can't figure out why now its happening..

    i have a table named discount and passing text value every time when text changes and save when click a button .. i know it happens because of this type of situation.

    This type of problem also happen if dataType is not matched correctly or some table was opened after working on current form...


    but can some one give me the cure... please help me.. great trouble...

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1720
    Your program executed an UPDATE statement and expected it to return that it had updated 1 row, but it returned that it had updated 0 rows.

    You should now find out why. We cannot tell you because we dont have the ability to look at your data and say "oh, look.. it's because Column X has changed recently"
    Note that this is called optimistic concurrency; you send the update HOPING that no-one else has changed the data youre trying to change. If you get this exception. It means someone else changed the data youre trying to change in the time between you SELECTing it and you UPDATEing it. How you proceed with that is up to you. Personally, I'd show the user: "Someone else changed the data to this: and here are your changes also: ... Chose which to keep/merge/overwrite/ignore"

    If youre still puzzled, do let me know why; I can't quite understand how it's a puzzle, you see.. If you tried to save a Word document and got the message "Another user already has this document open and you cannot save your changes" it would be fairly obvious what the problem is.. I find this error message to be similarly obvious and self explanatory, so knowing what is difficult or puzzling about it would really help me help others

  3. #3
    alim's Avatar
    alim is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    bangladesh
    Posts
    89
    Reputation
    110
    your concept and my concept is pretty similar but in one form i am the one who is working in one table and there is no one else.. so why this problem occur. i tried to change the Discount Column every time when a textbox textchange event raise... so do you think this is a cause for this exception?

    can you give me a sample or link for solve type of errors..

    and this view is not saving or working form view.. its a error details view of datatable..
    i m changes this tables discount on some other form where the Discount value changes everytime on textchange.. that all.. any suggestion/

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1720
    I don't know enough about your program to answer your question.. All I'm saying is:

    Between the time that you downloaded data into the datarow, and the time youre uploading the changed row, something else (it could even be your own program) has crept in and chenged the data in the db in such a way that the UPDATE statement cannot find any data to update.

    If every item of data that was downloaded is not in exactly the same value when the update comes, the update will fail. Have a look at the UPDATE statement in the adapter and you will understand

  5. #5
    alim's Avatar
    alim is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    bangladesh
    Posts
    89
    Reputation
    110
    i have solved my problem with saving those records each time textchanges..


    thanks
    alim

  6. #6
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    134
    If you create a record, then delete it without saving between the two, it creates a DataRow in the DataTable with the RowState of Deleted even though it has never existed in the database. It was only created in your local DataTable.

    When you try to update that DataRow, the TableAdapter will complain that the row's deletion could not be completed because it does not exist in the database. It will think the row was actually deleted by someone else while it was not looking when it actually never existed.

    I do not know if it is the source of your problem, but saving after every action would make the problem disappear (although you lose the benefits of a disconnected architecture) so I think it might be it.

    There are other ways to go around the problem, none really perfect, but you can put a condition to your deletion code to do table.Rows.Remove(myRow) instead of myRow.Delete() when its RowState is Added.
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  7. #7
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1720
    Stonkie, that's the biggest load of nonsense you've ever written. Deleting a row that is in rowstate Added causes it to be removed from the table.

    As an aside, where does your advice actually fit into the problem? The error message says "UpdateCommand affected...", and I'm sure you know that UPDATE is not used to remove rows from a table ergo discussing deletion of rows it not relevant to this context..

  8. #8
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    134
    I'm sure I actually solved an erroneous concurrency exception by adding a condition that does just that. I have no idea why or how it worked and I can't find that code anywhere now...

    I just spent an hour doing tests on the matter and I can't reproduce the behavior I was expecting! It's really my wrong, sorry.
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  9. #9
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1720
    I believe ya. Thousands wouldnt ;')

  10. #10
    Ali Asim is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Aug 2010
    Posts
    1
    Reputation
    0
    Its Simple.. You just need to update the Parent saperatly when there is this error.

    Consider the follwing example...

    Public Sub SaveItems()
    Try
    Me.Validate()
    Me.ProductsBindingSource.EndEdit()
    Me.ProductSuppliersBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.DsProducts)

    Catch DbconExp As System.Data.DBConcurrencyException
    Try
    Console.WriteLine("Fixing Concurrency Error...")
    Me.Validate()
    Me.ProductsBindingSource.EndEdit()
    Me.ProductSuppliersBindingSource.EndEdit()

    Me.ProductsTableAdapter.Update(Me.DsProducts.Produ cts)
    Me.TableAdapterManager.UpdateAll(Me.DsProducts)

    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End Sub



    Hope it will help someone like me >>>>>>>

  11. #11
    Todd Morrow is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2011
    Posts
    1
    Reputation
    0

    Not so simple

    Just because an error message has a simple (to some people) meaning, doesn't mean that debugging the issue is simple.
    In my case it certainly wasnt anything going on at the database that caused this error, it was not rubbing the ADO.NET objects the way they need to be rubbed,
    which is not necessarily intuitive.

    For example, You will also get this error if for whatever reason you don't call update on the data source datatable.
    the datatable has to be "reset" before calling update again.

    If you do something like:
    Code:
            Dim DataTableChanges = DataTable.GetChanges()
            If DataTableChanges IsNot Nothing Then
              SqlDataAdapter.Update(DataTableChanges)
            End If
    you will get the error.

  12. #12
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1720
    not 100% convinced that your code sample will always cause the error, what with that code being one example of a standard way to update a database and all - if you see this over message it really is because the database couldn't find any records to update

    ..PS the thread is 2 years old
    Last edited by cjard; 12-25-2011 at 7:36 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking