abbas abbas - 4 months ago 7
SQL Question

SqlDataAdapter doesn't delete a record

I use

DataSet
to store data and
SqlDataAdapter
to work with database.

for change records in DataBase first I edit rows (insert,Edit,delete) DataTable in Dataset ..

then

DataRow dr = DataSetMain.Tables["tbl_error"].Select("error_name='" + error.Name + "'")[0];
DataSetMain.Tables["tbl_error"].Rows.Remove(dr);
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM tbl_error", SVariable._DataBaseConnectionString);
SqlCommandBuilder bui = new SqlCommandBuilder(adp);
adp.Update(DataSetName,tbl_error);


for insert or edit record every thing work like a charm ...
but for delete it does not work ..

I`m sure row in tbl_error successfully deleted but adp.Update it won't delete anything from the database ...

how i can find problem ?

Answer

Calling Rows.Remove() is equivalent to call Rows.Delete() + AcceptChanges(). Due to AcceptChanges, Update() don't do any modification.

Then replace:

DataSetMain.Tables["tbl_error"].Rows.Remove(dr);

by

dr.Delete()