How to represent "null" datetime ?

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" ?
 
K

Kevin Spencer

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

SP

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
 
K

Kevin Spencer

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

Bruce Wood

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

John Richardson

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

Bruce Wood

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?
 
T

Truong Hong Thi

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
 
S

Scott Coonce

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top