0

I'm relatively new to Windows Forms and having trouble getting a form with multiple text fields to update the database.

I fill the dataset, add the data binding to each field and add a leave event to each field that updates the database.

clientsTableAdapter.FillByID(dataSetClients.Clients, tempID);

txtForename.DataBindings.Add("Text", dataSetClients.Clients, "Forename");
txtForename.Leave += new EventHandler(updateDataSet);

private void updateDataSet(object sender, EventArgs e)
{
    this.clientsTableAdapter.Update(this.dataSetClients.Clients);
}

The database does not update, I have tried this in many different ways and the only way that seems to work is if I update the dataset manually then run .update() on the adapter, like so;

this.dataSetClients.Clients.Rows[0]["Forename"] = "New Forename";
this.clientsTableAdapter.Update(this.dataSetClients.Clients);

Any help or guidance on the subject is greatly appreciated.

Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
baked
  • 235
  • 2
  • 9
  • 18

2 Answers2

2

The default DataSourceUpdateMode for DataBindings is OnValidation: as the Validation events aren't called until after the Leave event, the values aren't updated.

Specifying DataSourceUpdateMode.OnPropertyChanged for the DataBindings should work.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Although that was helpful, thanks, it didn't solve the problem.The data bindings arent the problem, when I run the Update() function on the adapter is when it dosnt work. – baked Mar 22 '11 at 14:10
  • @baked When you run Update() from Leave has the dataset been updated with the new value? – stuartd Mar 22 '11 at 14:14
  • @Stuart Yes, its just isn't updating the database with the (newly updated) dataset. – baked Mar 22 '11 at 14:28
  • 1
    @baked is the dataset's HasChanges property true? – stuartd Mar 22 '11 at 14:38
  • @Stuart No HasChanges is still set to false, I'm guessing the problem is the dataset then? Any idea what would cause that? – baked Mar 22 '11 at 14:52
  • @baked see http://stackoverflow.com/questions/1165477/net-dataset-haschanges-is-incorrectly-false/1165644#1165644 – stuartd Mar 22 '11 at 15:23
0

not sure if this problem is still on, I just want to contribute the things which helped me. I had the same problems with DataSet, Databinding and Data which was changed in the DataSet but not in the Database. I tryed out all this tips but nothing seemed to help.

DataSet.Data.Tables[0].Rows[0].EndEdit(); //0 for your Index/Tablename

That helped me, so I could bind the Data to the Dataset and then, when I wanted to Update the Data, I set "EndEdit()" to every table in the Current DataSet.

I also tryed this:

foreach (Control bla in DisplayDict[SubTyp.ToString()].DataControlHelper.Values)
{
    bla.BindingContext[DisplayDict[SubTyp.ToString()].DBData.Data].EndCurrentEdit();
}

which made no Change, and I also tryed .GetChanges() and .HasChanges() but the .EndEdit() was the one Thing missing in my Code.

I hope this helps someone.

P.S. Sorry for my bad english, it has been a while since I last used it (:

Kiwimanshare
  • 130
  • 9