I recently wrote an article that demonstrates how to use the DataForm and validate data entry in Silverlight using the DataAnnotations. One of the pet peeves I have with the validation is that it throws in exception in the setter of your public property. Putting my disagreements with that aside for a second, it causes a major inconvenience when debugging the application. For example, when running the application that uses the DataAnnotations and one of the conditions is violated, the debugger will catch the validation exception and break into the code. The desired behavior is that the debugger will not break into the code since I really just want to see the validation information on screen.
For example, the default behavior when not entering a value for the FirstName property will yield the result shown in the image below.
Yes, I can simply hit F5 and it will bypass the exception. But this gets ugly when you have more than one violation and you are testing the app. What I really want is to simply see the validation on screen, as shown below:
last night I was chatting with Jeff Handley (of DataAnnotations fame) and he mentioned a technique I was unaware of that will ignore the exception of this particular type. I was thrilled to see that it works and is quite simple to do. There is a feature in Visual Studio that allows you to do this (I love learning something new about Visual Studio). Here are the steps you need to go through to ignore the exception in Visual Studio:
1) Go to the Debug menu in Visual Studio, and select the Exceptions menu item
(If the images are hard to read, click on the images below for larger versions of them)
2) Expand the Common Language Runtime Exceptions item in the list
3) Click the Add button to add a new exception type
4) Select Common Language Runtime Exceptions from the drop down list
5) Enter System.ComponentModel.DataAnnotations.ValidationException in the Name textbox
6) Click OK
7) Uncheck the User-unhandled checkbox for the item
System.ComponentModel.DataAnnotations.ValidationException (the highlighting is mine)
8) Click OK
Now when you run your application using the debugger, the debugger will not stop no the exception. It instead will bypass those exceptions and simply display the validation information in your application. Basically skipping right to this:
Thanks to Jeff Handley for pointing this out to me!