1

So working on a project currently and stuck on this problem

When I try to update an item in the SQL database I keep getting an error and kind of stumped as to the error.

if (MessageBox.Show("Do You Wish To Update Field Details?", "Update Field Details", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
{
    try
    {
        int fieldID = int.Parse(cmbFieldInfo.SelectedValue.ToString());

        drFields = dsFarmCosts.Tables["fieldDetails"].Rows.Find(fieldID);

        drFields.BeginEdit();
        drFields["fieldName"] = add.FieldName;
        drFields["fieldSize"] = add.FieldSize;
        drFields["FieldLocation"] = add.FieldLocation;
        drFields["fieldHistory"] = add.FieldHistory;
        drFields.EndEdit();
        daFarmCosts.Update(dsFarmCosts, "fieldDetails");

        Reset();

        MessageBox.Show("Field Details Updated");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Error is

System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)

at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)

at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)

at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)

at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)

at Farm_Costs.field_screens.fieldEditScreen.btnSubmit_Click(Object sender, EventArgs e) in H:\School\Magee\Project\Programme\Farm Costs\Farm Costs\field screens\fieldEditScreen.cs:line 114

Amy advice or tips?

Liam
  • 27,717
  • 28
  • 128
  • 190
Andy
  • 11
  • 3

1 Answers1

1

You need to have an UpdateCommand on the daFarmCosts DataAdapter. Something similar to the following code:

...
daFarmCosts .Fill();
dadaFarmCosts UpdateCommand = new SqlCommandBuilder(daFarmCosts).GetUpdateCommand();
...
daFarmCosts .Update();
S.Dav
  • 2,436
  • 16
  • 22