GaryDean wrote:
Is there any good way in which to deal with null datetimes. For instance, I
can't seem to find anything that will pass a null value in a datetime
parameter to another method. Convert.dbnull will not work for instance.
Getting nulls back and forth between C# and SQL Server is always
problematic.
I wouldn't say that it's problematic, but you always have to write the
code for it.
When you get the value from the database, you first have to check if
it's a null value or not, before you can use the value.
Then you have to decide how you represent a null value in your code. A
null value from the database comes as a DBNull object into your code,
but that is mostly not a practical way to handle it in the code.
For a DateTime value you can use a nullable DateTime to handle the value:
DateTime? createdDate;
if (reader.IsDBNull(0)) {
createdDate = null;
} else {
createdDate = reader.GetDateTime(0);
}
To use the nullable value, you can check createdDate.HasValue to see if
there is a value, and createdDate.Value to get the value.
--
Göran Andersson
_____
http://www.guffa.com