May 20, 2008

LINQ Data Binding Gotcha

I was having a very strange problem where the last data bound field I changed on a form would not get saved to the database. I have a save button in a toolbar on the top of the form that triggers the SubmitChanges() call on the context. I would notice that if I only changed one field no SQL update was issued. If I changed two fields only the first field had an update. I traced through the problem and discovered that the property set method on the field was not getting triggered for the last field when I clicked on the save button. If I hit Tab before clicking on the Save button the field would get saved. Of course there is no way I'm going to be able to impose this as a requirement on the users.

It turns out there is a fairly simple way to fix this. The default update mode on a bound field is OnValidation. However it can be changed to OnPropertyChanged as shown in the dialog at left. Once I did this the behavior of the form was much more intuitive.

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.