FormView and ObjectDataSource with nullable types
I recently ran into this problem when using a FormView and and ObjectDataSource where the FormView was binding to nullable data types... Here is a challenging question, as I have not found any suitable information on this after two days of searching:
> Background: _I have a custom Business Class library that includes custom objects with nullable int and DateTime properties.
In order to update this data, I have created a FormView control bound to an ObjectDataSource that retrieves a generic List collection of my business objects.
The reflection that is intrinsically called by the FormView/ObjectDataSource combination dutifully builds my basic Select, Update, and Insert templates.
Now, when you edit the various (TextBoxes by default) input fields and then call the Update method referenced in the ODS (ObjectDataSource), the ODS attempts to convert the data contained in the input fields to the corresponding Type of the property bound to said input field.
This is where we run into our little problem..._ _Even though I have a nullable Integer property type (let's call it int? ClassNumber), if the corresponding TextBox is empty (since it's not required), an exception is thrown by the ODS since it tries to convert an empty string value to an integer value before trying to set the object's Property.
Hah! If ODS was able to determine that the integer is in fact nullable, it should pass in a null value instead! But alas, it throws a System.IndexOutOfRangeException: "Index was outside the bounds of the array" error.
And further down the stack: "Exception: is not a valid value for Int32".
Hmmm.
Since this is a FormView control, we don't have the ability to use a BoundField control with 2 very useful properties: NullDisplayText and ConvertEmptyStringToNull.
It would be nice to tell ODS to enable sending null values from any control we want._
The above was posted originally here.
I used his clipping because it describes perfectly the problem I had, along with many, many others.
After hours and hours of Googling and trial and error I finally found the solution to this.
There was no way in hell I was going to accept the solution of manually re-populating each field I had bound in my edit and insert templates for my FormView.
The solution was much easier and left all of the heavy lifting to the FormView.
Basically, all you have to do is all insert and update parameters in your ObjectDataSource for each field you're binding.
For these fields, you just need to supply the field name, the data type and set the ConvertEmptyStringToNull property to TRUE.
Also, you'll need to be sure to set the ConvertNullToDBNul property to TRUE in the ObjectDataSource itself, like below.
<InsertParameters>
<asp:Parameter Name="TimeZoneOffset" Type="int32" ConvertEmptyStringToNull="true" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="TimeZoneOffset" Type="int32" ConvertEmptyStringToNull="true" />
</UpdateParameters>
So just by adding these parameters for the insert and update you can have your FormView's databinding work with nullable types.
Not too bad, huh?