Saturday, February 28, 2009

WinForm databinding doesn’t support conversion errors?

I must be missing something, because I can’t believe the designers of WinForm data binding would fail to address the very common scenario of conversion errors: After all, it’s only expected from our users that they will enter invalid data, eventually.

When data is entered correctly, our objects are updated accordingly. Even custom types are updated, as long as the proper TypeDescriptor is registered.

Then we have the ErrorProvider which does an excellent job in marking any field violating a business rule.

But when there’s an error, say text is entered where numbers are expected, then the conversion error which happens during data binding is silently swallowed. Not only is swallowing exceptions considered bad practice, but I haven’t found any place to retrieve this information when it happens.

So what happens when an object contains a valid value and our user enters an unconvertible value? There are no exceptions, no events, the ErrorProvider doesn’t know what just happened so our field is never marked for error.

Does this mean the object will be saved with the old value? That’s not good, is it? This object is definitely saved in an inconsistent state.

I am asking for help on StackOverflow. Will you answer it, please? Until then, I am busy reinventing the wheel.

No comments: