Cannot Delete Rows in Database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I am using the following to remove a row from a DataSet:
myDataSet.Tables["Table1"].Rows.RemoveAt(indexSelected);

followed by;
myDataAdapter.Update(myDataSet);

After this call, looking in the database, the row is not deleted. But
debugging it I can see it remove the row.

Also note that the following insert does insert into the database:
myDataSet.Tables["Table1"].Rows.InsertAt(newRow, index);

Strange how InsertAt works but not RemoveAt.

Does anyone knows what the problem is????

Thanks for any help,
Scott
 
Do you mean to say it removed the row from the table when you debugged
it? (or removed it from the data table?)

Try using AcceptChanges method before updating the dataset.
 
Hi Scott,

Use DataRow.Delete instead of Remove[At].
The difference is that Remove[At] removes row from collection, while Delete
marks is as Deleted and it is picked by Update for deleting.
And don't call AcceptChanges before Update. Actually you don't have to call
AcceptChanges at all in your scenario.
(AcceptChanges consolidates RowState of all rows to Unchanged).
 
Hi Miha,

Thank you!
That was the problem.

Miha Markic said:
Hi Scott,

Use DataRow.Delete instead of Remove[At].
The difference is that Remove[At] removes row from collection, while Delete
marks is as Deleted and it is picked by Update for deleting.
And don't call AcceptChanges before Update. Actually you don't have to call
AcceptChanges at all in your scenario.
(AcceptChanges consolidates RowState of all rows to Unchanged).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Scott said:
Hi all,

I am using the following to remove a row from a DataSet:
myDataSet.Tables["Table1"].Rows.RemoveAt(indexSelected);

followed by;
myDataAdapter.Update(myDataSet);

After this call, looking in the database, the row is not deleted. But
debugging it I can see it remove the row.

Also note that the following insert does insert into the database:
myDataSet.Tables["Table1"].Rows.InsertAt(newRow, index);

Strange how InsertAt works but not RemoveAt.

Does anyone knows what the problem is????

Thanks for any help,
Scott
 
Back
Top