1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nikki Locke
  • 2,759
  • 6
  • 29
  • 53
  • 2
    Your chosen path works with MS-SQL but may be not (well) supported for other Db's. Keep it simple, don't rely on fancy functions too much. Auto number columns are not simple. How do you retreive a freshly added record? You may need an extra Guid column. – H H Jul 26 '11 at 20:40
  • Thanks for the input - I thought no-one knew about these things. I'm not sure how a guid would help - presumably I would have to add some code somewhere to generate the guid, and then get it back into the recordset - which brings me back to the original question of how I hook in to add this code? – Nikki Locke Aug 04 '11 at 17:04
  • Nikki - this is older stuff, look for articles etc <= 2005. When using MS-SQL a generated Insert statement consists of 2 parts, an insert and a special "give me back what I just Inserted". That solves a tricky problem: How do you get the server-generated key? Adding a client-generated GUID column can solve that too. – H H Aug 04 '11 at 17:17

0 Answers0