Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Nullable Dates

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wannabe - 13 Sep 2007 15:12 GMT
I have a start and end date in my application. If a user does not know their
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a nullable
date, and want to return it in my method.

I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateTime?)command.Parameters["@startDate"].Value,
(DateTime?)command.Parameters["@endDate"].Value);

Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable dates?

DateTime? startDate;
DateTime? endDate;

if (command.Parameters["@startDate"].Value is DBNull)
    startDate = null;
else
    startDate = (DateTime?)command.Parameters["@startDate"].Value;

if (command.Parameters["@endDate"].Value is DBNull)
    endDate = null;
else
    endDate = (DateTime?)command.Parameters["@endDate"].Value;

return new TimeLine(startDate, endDate);
Jon Skeet [C# MVP] - 13 Sep 2007 15:20 GMT
> I have a start and end date in my application. If a user does not know their
> dates yet, I want them, they will be null in the DB and I want them to be
[quoted text clipped - 7 lines]
> Then I changed to the code to what you see below, and it worked. But I am
> wondering is there a better way to do this when working with nullable dates?

The simplest way would be:

DateTime? startDate = command.Parameters["@startDate"].Value as
DateTime? ;
DateTime? endDate = command.Parameters["@endDate"].Value as
DateTime? ;

That will work, *but* it will give you null for start/end dates which
are neither DBNull nor DateTime. In other words, it will hide the
error. If you want more rigour, I'd write a helper method to convert
object to DateTime, throwing an exception if the parameter is neither
a DateTime nor DBNull.Value.

Jon
Ignacio Machin ( .NET/ C# MVP ) - 13 Sep 2007 20:36 GMT
Hi,

>> I have a start and end date in my application. If a user does not know
>> their
[quoted text clipped - 23 lines]
> object to DateTime, throwing an exception if the parameter is neither
> a DateTime nor DBNull.Value.

If the columns in the DB are of DateTime then it will work as expected.
Jon Skeet [C# MVP] - 16 Sep 2007 20:12 GMT
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions.com>>
> > That will work, *but* it will give you null for start/end dates
> > which
[quoted text clipped - 4 lines]
>
> If the columns in the DB are of DateTime then it will work as expected.

Indeed. I was just warning that if the columns were changed
inappropriately, it would fail silently (by returning null) rather than
screaming from the rooftops about the type being wrong :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.