Survey: Make SelectedDate Nullable of DateTime?

So ever since I introduced the concept of “nullability” for the CalendarPopup and TimePicker controls, everyone has continuously asked the same basic question:

“Why is it when I try to bind my data source to SelectedDate, I get the error ‘The type DBNull cannot be converted to the type DateTime’? How can I bind it to this field?”

So the answer is, well you have to make a method call to see if the value is NULL, and return DateTime.MinValue instead. Wow, it seems so simple – this is where I believe the design flaw comes in…if it’s so darn simple, why do I keep getting asked the same question?

So, obviously, with the advancements in .NET 2.0, and the introduction of Nullable<T>, I have the opportunity to nick this in the butt so I don’t have to field this question so often. However, this would require a huge breaking change from the current version to Server Controls v2.0 in the sense that SelectedDate / SelectedTime would now become Nullable<DateTime>.

Now, if you know your CLR internals well enough, typeof(Nullable<DateTime>) != typeof(DateTime). This means that existing code where you could do .SelectedDate.ToShortDateString() no longer “just works” because you have to take it 1 step further: .SelectedDate.Value.ToShortDateString().

So, being that I develop the controls for the community, how big of a hit would it be if modifying the current signature of SelectedDate / SelectedTime to be Nullable<DateTime>? Let me know by utilizing the comment’s feature to leave your vote. Thanks, and I appreciate your honesty.


Are you living under a rock? No? Did you see that Excentrics World Server Controls v2.0, RC1 is out in public? Check it out!

7 Comments

  • Hi Matt



    As one of the people who mithered you about this, I would love the feature!



    Using the DetailsView component, I've found it impossible to use the control with Dates in a DB that can be Null.



    It would make the control a lot more usuable, and it's only a small change in usage for those that are re-writing stuff.



    Hope we get it!



    Thanks



    Martin

  • ::I've found it impossible to use the control with Dates in a DB that can be Null



    I think that simply sums it up. A solution HAS to be found. Not supporting NULL has been a tremendous error in the first place.

  • And I thought I was the only one with this problem. There are some rather simple, but not intuitive problems to this issue.

  • Simple solution.



    Add a SECOND property called SelectedDateNullable which gets/sets a Nullable&lt;DateTime&gt;. Leave SelectedDate as a DateTime. Both properties would use the same ViewState items.



    No existing code is broken (that wasn't already broken), and people who want to bind to a data source have that option. Everyone is happy.



  • James, I thought of that same situation this morning. I already have an protected property that is Nullable&lt;DateTime&gt; that I believe I'll just expose. I think that's the route I'll be going.

  • Alright, I've exposed &quot;SelectedValue&quot; to both the CalendarPopup and TimePicker that allows binding with null values. This will be available in the next RC.

  • Hi,



    In my case it would be great being able to have the value as null - particulary if this was the default (ie. no date selected) because it would allow for date validation (to check if the person had actually made a selection). Maybe you could add an extra property to select between the nullable and non-nullable versions of the control.

Comments have been disabled for this content.