Undo after database command failsUndo after database command fails
Old forum URL: forums.lhotka.net/forums/t/7596.aspx
bloez posted on Wednesday, September 09, 2009
hi,
i need help 'cause have stucked in the following scenario: if my save operation on a business object fails, i want to revert all the changes happened since the last BeginEdit() command.
for example: the user deletes a row from a datagridview, then presses save button. the database operation fails and i want to put that deleted row back to the grid, since i don't want to confuse him and i think the UI state and database state has to be consistent.
i use BindingSourceNode. as i know if i call Apply(), i cannot anymore revert the changes:
private void Save()
{
_bindingTreeUnits.Apply();
try
{
_temp = _units.Clone();
_units = _temp.Save();
}
catch (Exception exc)
{
Mbox.Error(exc);
}
BindUI();
}
therefore i tried to use this:
private void Save()
{
Units _temp = _units.Clone();
_bindingTreeUnits.Apply();
try
{
_units = _units.Save();
}
catch (Exception exc)
{
_temp.CancelEdit();
_units = _temp;
Mbox.Error(exc);
}
BindUI();
}
but it doesn't work, it complains of Edit level mismatch in CopyState.
thanks
IanK replied on Thursday, September 17, 2009
You need to clone the object prior to the beginEdit which probably is set when you bind your object to the grid. If all your changes are saved to database as single save then clone object before binding it to the datagridview. If the Save method on your object fails then set the previously cloned object to the datasource of the grid and refresh the grid.
If you save each row individually then you need to clone prior to editing a cell in a row of the grid or prior to attempting the delete.
Copyright (c) Marimer LLC