Feed Subscribe
Exception has been thrown by the target of an invocation.

Silverlight DataForm’s autogenerated fields send empty strings to database

by ondrejsv 28. March 2010 17:18

Silverlight Toolkit comes with a great control for quick building typical business application screens – DataForm, a sibling control to the DataGrid for showing and editing a single record. If you use its field autogeneration feature, you may be surprised when looking to the database after its first submit. All your ((n)var)char columns rendered as TextBoxes in the DataForm that had been empty have also empty string values in the database:


This is not what you usually want. If the field is left blank, I expect the database field be NULL instead of an empty string. So what to do to persuade the DataForm to send nulls in this case?

An IValueConverter, of course! First we define a suitable one which returns null when converting back an empty string:

public class SourceNullValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value == null ? "" : value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (targetType != typeof(string)) return value; if (value == null) return null; return value.ToString() == "" ? null : value.ToString(); } }

Secondly, we need to force the DataForm to use this value converter instead of its own default DataFormToStringConverter. We do this by handling AutoGeneratingField event where we get the already built binding, copy it (by using CreateCopy extension method from the Silverlight Business Application template) and set our new value converter:

private void EmployeeForm_AutoGeneratingField(object sender, DataFormAutoGeneratingFieldEventArgs e) { if (e.Field.Content.GetType() == typeof(TextBox)) { Binding binding = e.Field.Content.GetBindingExpression(TextBox.TextProperty).ParentBinding.CreateCopy(); binding.Converter = new SourceNullValueConverter(); e.Field.Content.SetBinding(TextBox.TextProperty, binding); } }

Case closed, Poirot!

kick it on DotNetKicks.com [digg]

Tags: ,