I’am trying to update database after row has been deleted from dataset like this:
SqlCommand mySelectCommand = new SqlCommand("select * from NameDB", c);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySelectCommand);
TheNewDataSet ds = new TheNewDataSet();
mySqlDataAdapter.Fill(ds, "NameDB");
DataRow[] FilteredRow = ds.Tables["NameDB"].Select("PName like '" + listBox2.SelectedValue.ToString() + "'");
foreach (DataRow drr in FilteredRow)
{
ds.Tables["NameDB"].Rows.Remove(drr);
}
mySqlDataAdapter.Update(ds, "NameDB");
c.Close();
And nothing happened to database, rows not deleted. According to debugger rows filtered correctly and deleted from DataSet normally. Where is mistake?
Ok, first – it is not the dataset doing updates. NEVER EVER. It is the Adapter. The DataSet does not talk to the database.
But: you dont delete the rows, you – kill them (rows.remove).
http://msdn.microsoft.com/en-us/library/feh3ed13(v=vs.80).aspx
You should not use REMOVE but DELETE, which keeps the row with a status to delete it. THe way you do it you remove it, so the adapter never sees it. With “.Delete” the adpater sees the row as deleted, so it can act upon it.