Concurrency violation: the UpdateCommand affected 0 of the expected 1 records with autonumber fields

不想你离开。 提交于 2019-12-11 05:06:15

问题


I am writing a small C# society membership app using Visual Studio 2008, and the new data layer with .Net 3.5, along with SQLite.

I used the various wizards to generate my main form, which has a BindingNavigator and a BindingSource on it. The idea is that the form shows 1 record, and you can update the record, and use the BindingNavigator controls to navigate to the other records, add records, delete records, etc.

I would like the current record to be written back to the database whenever the user navigates to a different record (or closes the form).

The member records have an autonumber field in them (I intend to have relationships with other tables elsewhere in the app).

That all works, except when I add a new record. The new record is saved, but any attempt to update data thereafter gives the error message in the title.

I realise this is because the autonumber field has not been added to the local copy of the dataset when the data is saved. But I am at a loss as to where to put some code to retrieve the autonumber id and stuff it in the new record.

I could put the code in the Insert method of the generated code for the data table, but my changes would be lost next time I changed anything in the designer, plus I am not sure the Insert method actually gets called (putting a breakpoint there doesn't seem to stop).

I Googled for an answer, and found a suggestion that I check the "Refresh after update" checkbox in the advanced configuration settings in the data table designer, but that did not change the generated code, or have any effect (and when I went back to look, the checkbox was unticked again).

Supplementary question - where is the best place to read an overview of how DataBindingNavigator, DataBindingSource, and the new generated data classes all fit together?

Finally, apologies if you see another, similar question from me. I could have sworn I asked one yesterday, but I can't find it here, so I am assuming it never got posted.

来源:https://stackoverflow.com/questions/6836181/concurrency-violation-the-updatecommand-affected-0-of-the-expected-1-records-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!