Handling Windows Forms Data Binding Errors
 
Published: 01 Aug 2007
Abstract
This article shows you how to make sure you find out about any errors and problems that might occur during data binding.
by Jon Kruger
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33443/ 99

Introduction

Data binding is great, but one thing that newbies do not know is that data binding will eat exceptions and never tell you - unless you know what events to listen to.

Back in the .NET 1.1 days, this was really bad because there was no way to find out that these exceptions were happening. So you would sit there for hours wondering why things were not working when everything looked fine.

What can go wrong?

There are a few different things that can go wrong when you use data binding.

·         Exception when setting the value

·         Value assigned to the property fails validation (but does not throw an exception)

·         Exception thrown when data binding cannot call the setter

·         Exception thrown by someone handling a CurrencyManager event

What can I do about it?

If you use the BindingSource component to do data binding, then this will not be too hard. If you are not using the BindingSource component, then I would highly recommend that you do because otherwise things are going to be a lot more complicated and you might have to handle a lot more events.

Here are the two events that you need to be concerned with (all on the BindingSource component):

·         BindingComplete - This fires when a binding operation is completed (that is, after it is done trying to push the value into the property). The event args have a BindingCompleteState property that returns one of three states: Success (if everything went OK), DataError (if it is able to push the value into the property but the value failed validation), and Exception (if an exception was thrown for any reason).

·         DataError - This event will report exceptions that happen during CurrencyManager events (usually because someone handles an event like CurrencyManager.CurrentChanged and does something that causes an exception).

Do not confuse the DataError event with the BindingCompleteState.DataError value in BindingComplete. You might think that the DataError event will fire when a value fails validation, but this is not the case.

If an exception occurs in the setter of the property (or something called by the setter) or if there is an exception thrown by the reflection code that is trying to call the setter, you might see odd behavior in your controls, such as your control not letting you change focus to anything else. If you ever see this behavior and you are not sure why, handle the BindingSource.BindingComplete event and you will probably find out what is going on.

It is not a bad idea to handle the BindingComplete and DataError events just so that you can be aware when these exceptions occur, even if it is just for debugging purposes. Otherwise, bad things could be happening and you might not know it.

Tips for using grid controls

 

Grid controls work a little bit differently. For example, the DataGridView exposes a DataError event, which reports information about exceptions that happen during data binding. If you do not handle the DataError event, the grid will pop up a message box explaining that an exception occurred and that you should handle the DataError event. At least this way you know if something went wrong.

Downloads

Conclusion

In this article you have learned how to handle some of the data binding errors in Windows Forms applications.

 

 

 



User Comments

Title: Easy open source alternative to data binding   
Name: Michael L Perry
Date: 2008-04-09 10:29:50 PM
Comment:
Data binding can be complicated and error prone. Thanks for posting this quick guidance on making it simpler.

There is, however, and easier way. Please check out this open source alternative to data binding at http://codeplex.com/updatecontrols. Update Controls are Windows Forms controls that update themselves when your data changes. You don't have to bind the controls through a binding source. They can work with any business logic you want to use.

To use Update Controls, you don't bind to a property. Instead, you handle an event. For example, the UpdateTextBox control fires the GetText event to determine what string to display. You can call any business logic to figure out this string. The control pays attention to all of the data that you access during that event. When that data changes, the event is fired again. It's all automatic, and works with any business logic, no matter how complex or indirect.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-10-20 12:55:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search