DataSet changes not being persisted to database
I have a windows form with a number of data-bound fields relating to a client. Additionally, I have a DataGridView showing orders for the currently displayed client. This is achieved using the design-time Visual Studio controls (e.g. a project DataSet, BindingSources TableAdapters and a TableAdapterManager.
When loading the form to edit an existing client it is populated using a (custom) FillByID method on the ClientsTableAdapter to filter the DataSet to the single, relevant client. This loads their details into the client fields, and populates the orders DataGridView correctly.
Any changes I make to a client's orders (within the DataGridView) are persisted to the database when saving, however, none of the client detail fields are. They are editable, and show the correct details when loading, but no changes that are made are saved when the form is closed. I have tried two separate ways of persisting them now, the first being inline with the order saving:
this.Validate();
this.clientordersBindingSource.EndEdit();
this.clientsBindingSource.EndEdit();
this.clientordersTableAdapter.Update(projectDataSet.clientorders);
this.clientsTableAdapter.Update(projectDataSet.clients);
...and the second using the multiple table example on MSDN. Sadly, neither of these methods work with regards to the client details - although changes to client orders are persisted in both case开发者_StackOverflow社区s.
My clients table has the following structure:
refnumber, title, forename, surname, gender, dob, contactnumber
The clientsTableAdapter has the following CommandText strings for the DeleteCommand, InsertCommand, SelectCommand and UpdateCommand respectively:
DELETE FROM clients WHERE (refnumber = @Original_refnumber)
INSERT INTO clients (refnumber, title, forename, surname, gender, dob, contactnumber) VALUES (@refnumber, @title, @forename, @surname, @gender, @dob, @contactnumber)
SELECT refnumber, title, forename, surname, gender, dob, contactnumber FROM clients
UPDATE clients SET refnumber = @refnumber, title = @title, forename = @forename, surname = @surname, gender = @gender, dob = @dob, contactnumber = @contactnumber WHERE (refnumber = @Original_refnumber)
When saving, if I add a variable to capture the result of this line:
var res = this.clientsTableAdapter.Update(projectDataSet.clients);
projectDataSet.clients.Count == 1
projectDataSet.clients.Rows[0].ItemArray
shows the modified fields.projectDataSet.clients.Rows[0].RowState == Unchanged
res == 0
Please can anyone offer any hints? This has been driving me insane for days!
UPDATE
I recreated the form from scratch using the Visual Studio designer, then copied & pasted all the code from the original form. It now works. I guess something must have been wrong in the Designer.cs file with one of the datasources/databinding fields.
Thanks for the helpful debugging hints, ewall and Ladislav Mrnka.
What are you saving too for a datasource? Second, make sure your tableadapters have valid update, insert, and delete commands. I have had issues where they are not filled out even though the wizard said it had done it.
精彩评论