Instead of line
ds.Tables["employee"].Rows.Remove(row);
just use
row.Delete();
The logic behind is that
DataTable is just an off-line in-memory object representing data from your real DB table. You filled real DB
employee table into your off-line
DataSet ds object by calling
da.Fill(ds, "employee");
Then you reference the filled table, stored in
ds DataSet, named "employee", by the code fragment
ds.Tables["employee"]
This returns the object of type DataTable. Consequently,
DataTable.Rows Property is just an
off-line in-memory collection of
DataRow objects filled from your real DB. You can manipulate each DataRow object by changing its value. Changes are tracked by
DataRow.RowState Property and various versions of field values are accessible through
DataRow.Item Property (String, DataRowVersion). Nothing reflects in real DB so far. Until you call
da.Update(ds, "employee");
Performing
DataAdapter.Update Method, data adapter walks through your off-line DataTable.Rows collection of "employee" table. For each
row (of DataRow type),
row.RowState is one of
DataRowState Enum. If adapter finds
row.RowState = DataRowState.Added then it tries to call Insert command on real DB, filling in values from this off-line
row("current" version of each corresponding field). If
row.RowState = DataRowState.Modified, data adapter tries to call Update command on real DB, matching the
row.PrimaryKey with the real primary key of the real DB table.
If row.RowState = DataRowState.Deleted, the data adapter tries to call Delete command on real DB, matching the
row.PrimaryKey with the real primary key of the real DB table. If
row.RowState = DataRowState.Unchanged or the
row was removed from the Rows collection, then data adapter does nothing in real DB for matching (or missing) row..
So this is it. As you completely removed your off-line representation of your real DB row by calling
ds.Tables["employee"].Rows.Remove(row);
then the data adapter
da has no reference to that real DB row anymore.
On the other side, if you find your off-line DataRow
row object and call
row.Delete();
then the
row.RowState is set to
DataRowState.Deleted and the consequent
da.Update(ds, "employee");
finds the
row marked
Deleted and calls the Delete command on real DB.
Hope this helps,
madvad