I’m having major problems trying to clear a DataTable in C#.
At all other points when editing information in the database, I’ve used something like
somethingTableTableAdapter1.Update(greenParksDataSet);
greenParksDataSet1.AcceptChanges();
and this has worked just fine. However, in one of my forms I have a button to clear the open table. It seems to clear the DataTable in memory, but not actually propagate the changes to the database. Essentially the code boils down to
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = -1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = -1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = 1;
greenParksDataSet.HistoryTable.RecieptIDColumn.AutoIncrementSeed = 1;
greenParksDataSet.HistoryTable.Rows.Clear();
historyTableTableAdapter.Update(greenParksDataSet);
greenParksDataSet.AcceptChanges();
I’ve tried messing around with the order of things but nothing seems to work. With that code on its own, the DataTable is cleared (and the changes are shown in the DataGridView) but when adding a historyTableTableAdapter.Fill(greenParksDataSet.HistoryTable); after that, it is shown that the database is not being changed whatsoever.
I’ve probably missed something obvious, but I’ve been puzzling over this for ages, and any help would be appreciated. Thanks!
Have a look at this question/answer.
Clear just works on the datatable in memory – it does not flag them for deletion.