473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Nullable Dates

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);

Sep 13 '07 #1
3 1921
On Sep 13, 3:12 pm, Wannabe <Wann...@discussions.microsoft.comwrote:
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?
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

Sep 13 '07 #2
Hi,

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Sep 13, 3:12 pm, Wannabe <Wann...@discussions.microsoft.comwrote:
>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?

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.
If the columns in the DB are of DateTime then it will work as expected.
Sep 13 '07 #3
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions.com>>
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.

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

--
Jon Skeet - <sk***@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
Sep 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
0
by: tg.foobar | last post by:
i'm using a propertygrid to show / edit values of a class that i have. I'm using the new(er) nullable types and it's showing the value of these items as empty (as i would think), but the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
Cathode Follower
by: Cathode Follower | last post by:
In VB6 we put date values obtained from databases into variants. We can then use statements like IsNull(.EndDate) to check whether a date is set or not. When you put code like this through the vb.net...
2
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.