Windows Forms data binding bug? (CSLA 3.8.3)

frankhoffy posted on Tuesday, July 20, 2010

I've encountered what appears to be a bug with the CancelEdit functionality in a Windows Forms app.

Steps to reproduce:

Launch Project Tracker (PTWin) and bring up the project edit form (any project will do)

Type some stuff into the description field

Shut down your SQL server instance

Hit the apply button (this will cause an exception from SQL server)

Restart your SQL server instance

Hit the Cancel button. Nothing happens. The original text should have been restored for Description at this point.

I've found that when the object is in this state, it has an EditLevel of 0, which is why CancelEdit isn't working. Any exception thrown from the database layer will cause this same behavior. I tested this by calling RAISERROR from the stored proc.

Any workarounds would be appreciated.

RockfordLhotka replied on Tuesday, July 20, 2010

That's actually standard behavior.

What happens is this:

The user clicks Save

That means ApplyEdit() is called on the object graph, committing all user changes to the object in memory

The object is then cloned

The clone is then saved

The exception occurs, so the clone is discarded

The UI is left referencing the original object because Save() errors out

If you want different behavior, you'd need to grab a clone of your object graph before the ApplyEdit() call, then catch the exception from Save() and bind the UI to the clone.

frankhoffy replied on Tuesday, July 20, 2010

Very interesting. Any suggestions for how I can grab the new object instance if I'm using a CslaActionExtender?

frankhoffy replied on Wednesday, August 18, 2010

I think I'm very close but I keep running into an exception. I'm using CslaActionExtender. I clone the object in the ObjectSaving event. I trap the ErrorEncountered event to handle exceptions from the database server. In that event, I call ResetActionBehaviors using the clone (this is because data bindings are lost when the exception occurs). When I hit the Cancel button, it works fine on the parent, but the child list collection is giving me an edit level mismatch exception.