0

I have a database and after I delete a row I cannot update database, I'm getting error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll update requires a valid updatecommand when passed datarow collection with deleted rows

... indicating line: sqliteDataAdapter.Update(dataTable); Here's my code:

private void SaveChanges(bool keepControlsDisabled)
{
    disableControls();

    Validate();
    programs_dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
    programs_dgv.EndEdit();

    programs_dgv.DataSource = null;
    programs_dgv.DataSource = bindingSource;
    programs_dgv.Update();

    sqliteDataAdapter = new SQLiteDataAdapter();
    sqliteDataAdapter.Update(dataTable);
    dataTable.AcceptChanges();

    if (keepControlsDisabled == false)
    {
        enableControls();
    }
}

Here's the delete code:

private void contextMenuStrip_tsmi_Click(object sender, EventArgs e)
{
    Int32 rowToDelete = programs_dgv.Rows.GetFirstRow(DataGridViewElementStates.Selected);
    programs_dgv.Rows.RemoveAt(programs_dgv.CurrentCell.RowIndex);
    programs_dgv.ClearSelection();
}

I've read the question at Saving changes from a DataGridView back to an SQL database? but that didn't help.

  • Is there a universal save function to pass records from datagridview to database?

  • Am on the right way? Do I have to deal with database and display results on datagridview? Or firstly datagridview must be handled? I'm so confused with that. Which one is right for performance?

onurcano01
  • 21
  • 2
  • It sounds like the datasource update command is not set. (SqlDataSource.UpdateCommand) you should be able to set this in the designer view, once it has a valid update statement to run you shouldn't receive this error. – user685590 Aug 23 '17 at 22:02

1 Answers1

0

Have you used deletecommand

I think it is more of a sql program and you can easily figure out from debugging.

or see of this helps https://stackoverflow.com/a/18578486/1481690

Peru
  • 2,871
  • 5
  • 37
  • 66
  • https://gist.github.com/nikel/a1ef459a16c1275b48a937414673b135 I tried this but this time it doesnt delete anything. – onurcano01 Aug 23 '17 at 22:53