Usually, I use FormClosing event to check if there is any pending change in database tables. If yes, I call the TableAdapter.Update(Datatable) method and DataTable.AcceptChanges() before exit, to commit all pending changes.
For this purpose I have a SaveData() function.
In the form's menu I've added two buttons, [Exit] button and [Save and Exit] button. The [Exit] button simply closes the form, FormClosing is fired and if necessary save current data. [Save and Exit] calls SaveData() function and closes the form, and, FormClosing if fired again.
I don't know why, but even if I called SaveData() , Datatable.GetChanges() returns always values. As far as I know, AcceptChanges() must mark all updated records.
I'm using a typed dataset object generated with V.S.
Do you know if there Is any issue with Datatable.AcceptChanges() with typed dataset?
Thanks.
public bool OrdersChanged
{
get
{
DataTable c = this.orders.GetChanges();
return (c == null || c.Rows.Count <= 0) ? false : true;
}
}
public bool UpdateAll()
{
try
{
ta_orders.Update(this.orders);
this.orders.AcceptChanges();
}
catch (Exception ex)
{
this.last_error = ex.Message;
return false;
}
return true;
}
protected override void AditionalOnClosingActions(FormClosingEventArgs e)
{
base.AditionalOnClosingActions(e);
this.Validate();
this.gridQuestions.EndUpdate();
this.bl_order.bsOrders.EndEdit();
if (this.bl_order.OrdersChanged)
{
string msg = this.Text + "\n\nSave pending changes?";
if (MessageBox.Show(msg, "CMF", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
SaveData();
}
}
}
protected override bool SaveData()
{
base.SaveData();
bool result = false;
try
{
this.Validate();
this.bl_order.UpdateAll();
if (this.bl_order.last_error != "")
throw new Exception(this.bl_order.last_error);
SendParentRequest("refreshOrdersList", null);
result = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return result;
}