Remove Row through DataAdapter

Alex picture Alex · Nov 29, 2010 · Viewed 12.8k times · Source

I've initialized a dataAdapter :

string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");

And this works just fine. The problem is when i try to delete a record and update the database. I remove a record from the dataset as such :

ds1.Tables["localitati"].Rows.Remove(dRow);

And this works just fine as well(verified).

The problem is when i update the DataAdapter, the DataBase doesn't get modified :

con.Open()
da1.Update(ds1, "localitati");
con.Close();

What could be the problem ?

Answer

Rami A. picture Rami A. · Aug 21, 2012

What fixed it for me was to call the Delete method on the DataRow instead of the Remove method on the DataTable.

ds.Tables["localitati"].Rows.Find(primaryKeyValue).Delete();

or just simply

dr.Delete();