How to represent "null" datetime ?

  • Thread starter Thread starter craigkenisston
  • Start date Start date
C

craigkenisston

I have a generic function that receives a couple of datetime values to
work with. They can or cannot have a value, therefore I wanted to use
null. This function will call a database stored procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?
 
You can't insert a null value into the database. But if the column is
nullable, what you *can* do is *not* insert a value into it. For example,
you can write a query or Stored Procedure that omits that column from an
INSERT statement.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Kevin Spencer said:
You can't insert a null value into the database.

Actually you can.

But if the column is
nullable, what you *can* do is *not* insert a value into it. For example,
you can write a query or Stored Procedure that omits that column from an
INSERT statement.

How many stored procedures would you need to write for a table with 10 date
fields using that logic?!!!.

Write one stored procedure and if the DateTime value is MinValue then pass
System.DBNull.Value as the value for the parameter.

SP
 
My apologies. Haven't been working too closely with databases for the most
part lately, and have fallen behind in my knowledge. How embarrassing!

You are, of course, correct. I shall go and scourge myself immediately!

--
:-(,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Pre-2.0, you can use DateTime.MinValue or DateTime.MaxValue, depending
upon the situation. For something like a null expiry date (not expired
yet) you would use DateTime.MaxValue. In fact, I tend to use
DateTime.MaxValue much more, as it indicates "not yet", which is
typically what a null date and time means. For null date / times that
indicate "missing information" I imagine that DateTime.MinValue would
be more appropriate.

Starting in C# 2.0 (due out shortly), you will be able to specify the
parameters to your generic method as

DateTime? createDateTime

The question mark indicates a nullable value type.
 
One thing though, about the DateTime.MinValue that's so much fun, is that
the MinValue is outside the DateTimePickers control's legal bounds for a
date... yay! So be careful about binding your dates blindly to the UI
control.

What I would do is use 1900/1/1 instead... store it as a const somewhere and
test for that, instead of the MinValue.
This only applies if you have any GUI related stuff here, and TBH, it
doesn't sound like it applies in your case. Just thought I'd add my 2 bits.
 
Oh, yes: no promises about whether the version 2.0 DateTimePicker will
be able to handle DateTime? properly. I haven't heard one way or
another. Anyone already using 2.0 care to comment?
 
A simple and safe approach is to write separate methods (or overloaded
ones) to handle such special cases. If that ends up in too many
overloaded methods, how about writting a wrapper class for DateTime
structure?

Regards,
Thi - http://thith.blogspot.com
 
John Richardson said:
One thing though, about the DateTime.MinValue that's so much fun, is that
the MinValue is outside the DateTimePickers control's legal bounds for a
date... yay! So be careful about binding your dates blindly to the UI
control.


If it were me, I'd stick to DateTime.MinValue, since (in your case) it acts
like a -null- value for the UI. That is, you always have to be careful
about binding null values, and well, the same would be true for the
DateTime.MinValue.

Scott
 
Back
Top