views:

124

answers:

1

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 cases.

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.

A: 

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.

Wade73
It is saving to a SQLite database using the System.Data.SQLite component. All the TableAdapters have valid commands.
Rezzie
Do you have the file as part of your project? If so, is it set to copy to the output directory? I believe I ran into a similar issue with Access. The problem was that when I restarted the app it was blowing away my changes because it kept copying a clean version of the db over the one I had modified. Just a thought.
Wade73
Yes, the database is OK as changes to the other tables are saved correctly. It is only the clients table that won't save:/
Rezzie