TableAdapter.Update not working

Wesley picture Wesley · Mar 23, 2010 · Viewed 7.8k times · Source

Here is my function:

    private void btnSave_Click(object sender, EventArgs e)
    {
        wO_FlangeMillBundlesTableAdapter.Update(invClerkDataDataSet.WO_FlangeMillBundles);
        wO_HeadMillBundlesTableAdapter.Update(invClerkDataDataSet.WO_HeadMillBundles);
        wO_WebMillBundlesTableAdapter.Update(invClerkDataDataSet.WO_WebMillBundles);
        int rowsaffected = wO_MillTableAdapter.Update(invClerkDataDataSet.WO_Mill);

        MessageBox.Show(invClerkDataDataSet.WO_Mill.Rows[0]["GasReading"].ToString());
        MessageBox.Show(rowsaffected.ToString());
    }

You can see the fourth update in the function uses the same functionality as the rest, I just have some debugging stuff added. The first three tables are bound to DataGridViews and work fine. The fourth table has its members bound to various text boxes.

When I change the value in the text box bound to the GasReading column and click save the first MessageBox does in fact show the new value, so it's making it into the dataset correctly. However, the rowsaffected is always showing 0 and the value in the actual database is not being updated.

Can anyone see my problem? I understand that the problem must be elsewhere in my code since the four update methods are the same, but I just don't know where to start.

Here is my binding code:

        txtHours.DataBindings.Add("Text", invClerkDataDataSet.WO_Mill, "HoursRun");
        txtGasReading.DataBindings.Add("Text", invClerkDataDataSet.WO_Mill, "GasReading");
        txtDelayMins.DataBindings.Add("Text", invClerkDataDataSet.WO_Mill, "DelayMins");
        txtGapMins.DataBindings.Add("Text", invClerkDataDataSet.WO_Mill, "GapMins");

I understand there are more elaborate ways to bind, but the binding isn't the issue. Again, data is making it from the bound textboxes into the datatable, but they aren't making it from there to the database.

Answer

Henk Holterman picture Henk Holterman · Mar 23, 2010
  1. It is a good idea to call wO_MillDataSource.EndEdit() before Adapter.Update(). To pull in all changes from the controls.

  2. If there are Foreign Key relations between the 4th table and one of the others you will have to split up Insert/Update and Delete actions, or use a TableAdaptermanager