eWorld.UI - Matt Hawley

Ramblings of Matt

Survey: Make SelectedDate Nullable of DateTime?

May 17, 2006 08:33 by matthaw

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!


May 17. 2006 15:22

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!



Martin Dolphin

May 17. 2006 15:40

::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.

Thomas Tomiczek

May 17. 2006 19:07

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


May 17. 2006 21:35

Simple solution.

Add a SECOND property called SelectedDateNullable which gets/sets a Nullable<DateTime>.   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 Curran

May 17. 2006 23:06

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

Matt Hawley

May 18. 2006 11:21

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

Matt Hawley

May 18. 2006 12:44

Of course I don't recommend null dates in a database.  Rather, figure out what the date represents (a start date or an end date) and set it to the appropriate maximia.  My O/R mapper takes care of that translation in a database-independant way.  This is important to do so that you can code queries as plain-old BETWEEN clauses (with no special-casing for NULLs). This REALLY simplifies life for the developer and for the SQL optimizer (as it can depend on things being in the index, etc.)

Oh, as for the actual question... don't change the property, add a new one.

IDisposable@gmail.com (Marc C. Brooks)

June 1. 2006 14:50


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 are closed

Copyright © 2000 - 2021 , Excentrics World