none
Master Details Concurrency violation: The updatecommand affected 0 of the expected 1 record RRS feed

  • Question

  • I have a master detail scenario setup on my Contacts form. The master table is Contact and has contact names, the details table lists all the phone numbers for each contact. Both Contact and Phone are displayed in datagridviews. I've added the ContactBindingSource.Endedit command to the addingnew event of the phone table and when I click the save button it runs:

    Me.Validate(), Me.ContactBindingsource.endedit(), Me.PhoneBindingSource.endedit() and me.tableadaptermanager.updateall(me.DataSetName)

    The problem occurs when I add a new contact and then add a phone number for that contact before saving the contact. When I run the save subroutine, the phone record gives me the following error: "Concurrency violation: The updatecommand affected 0 of the expected 1 record". The confusing part is that I am inserting a new record into the phone table, so I don't know why the update command is being run. After I receive this message, I can check back in the database and no changes have been made. I've used the SQL server profiler to see what commands are actually being run and it appears that the update Stored Proceudre is being run and -1 is being passed in as the PhoneID.

    I've tried a few tricks that have helped along the way such as changing the source for all the isnull parameters from Current to Original and this did not do the trick either.

     

    Monday, November 29, 2010 4:44 AM

Answers

  • I found the solution on my own. The reason for the abnormal behavior was that the Accept/Reject Rule for the relationship in the dataset within this parent/child was set to "Cascade" instead of "none" once I changed the rule back to "none". It worked without a problem.
    • Marked as answer by Technobeam Wednesday, December 1, 2010 6:57 PM
    Wednesday, December 1, 2010 6:57 PM

All replies

  • i have a little more information on this issue. For some reason the dataset is running the Update command when it should be running the Insert command. Obviously it is going to get a concurrency violation because if you run the Update command on a record that does not exist, the dataset assumes that another user deleted the record, thus the concurrency violation. I'm still not sure why it is running Update instead of insert. The record is a new row in a datagridview and should definately be interpreted as Insert.
    Wednesday, December 1, 2010 5:54 PM
  • I have one more observation. This only happens when the master record has not yet been saved. If the master record is saved first, then the detail record performs the Insert as expected. However, if the master record has yet to be saved, the detail record runs the update command instead of the insert command and returns a concurrency error.
    Wednesday, December 1, 2010 6:32 PM
  • I found the solution on my own. The reason for the abnormal behavior was that the Accept/Reject Rule for the relationship in the dataset within this parent/child was set to "Cascade" instead of "none" once I changed the rule back to "none". It worked without a problem.
    • Marked as answer by Technobeam Wednesday, December 1, 2010 6:57 PM
    Wednesday, December 1, 2010 6:57 PM
  • how will you save the master record first /

    will you use ContactTableAdapter or TabelAdapterManager /

    what about primary key of master table will you call methods of DB to fetch generated id & then assign it back to Details rows/

    SANDEEP

    Wednesday, December 8, 2010 12:13 PM