lundi, mars 19, 2007

TableAdapter.Update does not save data

I am using a winform application and a DataSet, but Update method of the DataTable doesn't save data contained in my form's controls, after goggling over the Internet, the problem was in the DataSet binding mechanism, you must close the edit procedure by calling EndCurrentEdit(), before the update, Example :



this.BindingContext[this.objKPMGdonnées, "MyTable"].EndCurrentEdit();
foreach (MyDataSet.MyTableRow MyTableRow in objMyDataSet.MyTable)
{
try
{
switch (MyTableRow.RowState)
{
case DataRowState.Modified:
this.MyTableTableAdapter.Update(MyTableRow);
break;

case DataRowState.Added:
this.MyTableTableAdapter.Insert(
System.Convert.ToInt32(this.lblID.Text)
, MyTableRow.Libelle
);
break;

case DataRowState.Deleted:
int ID = System.Convert.ToInt32(MyTableRow["ID", DataRowVersion.Original]);
this.MyTableTableAdapter.Delete(ID);
break;

}
}
catch (Exception exp)
{
continue;
}
}


An other interesting thing on Delete case, to retrieve the ID of the row to delete, you must use DataRowVersion.Original, because there is two Dataset, the original one and the modified one.

Aucun commentaire: