469,927 Members | 1,909 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Handling null DateTime objects?

Platform : ASP.Net/C#

void SomeFunction(DateTime date)
{
string text = date.ToString(); //This crashes if date is null
}

I am using a construct similar to above. I want to check if the date is null
or nor before processing it, but I am not able to figure out the syntax.

Please suggest my approach.

I want something like:

if (date is null)
{
text = "";
}
else
{
text = date.ToString();
}

Thanks

Manish
Nov 15 '05 #1
8 18965
Manish Jain <ma******@hotmail.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(DateTime date)
{
string text = date.ToString(); //This crashes if date is null
}


date can't be null. DateTime is a value type, null is a reference.
Please come up with a short but complete program to demonstrate your
problem - see http://www.pobox.com/~skeet/csharp/complete.html for
details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Sorry about the incomplete info. I am using MSPetshop architecture.

In the SQL Server layer, I get the the data into a Data Model class:
if (reader.Read())
{
ShortTaskInfo item = new ShortTaskInfo();
item.TaskID = reader.GetInt32(0);
item.Subject = ( reader.IsDBNull(1) ? "" : reader.GetString(1) );
item.Details = ( reader.IsDBNull(2) ? "" : reader.GetString(2) );
item.DueDate = reader.GetDateTime(3);
}

But since I cannot pass back a null in DateTime, I am passing whatever I get
from the database. This field is optional, and can be null in database. I am
not able to handle these null values on my UI layer, and they are causing a
crash. I do not want to resort to workarrounds like passing
DateTime.MaxValue or DateTime.MinValue for null and checking for that on my
UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Manish Jain <ma******@hotmail.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(DateTime date)
{
string text = date.ToString(); //This crashes if date is null
}


date can't be null. DateTime is a value type, null is a reference.
Please come up with a short but complete program to demonstrate your
problem - see http://www.pobox.com/~skeet/csharp/complete.html for
details.

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

Nov 15 '05 #3
Manish Jain <ma******@hotmail.com> wrote:
Sorry about the incomplete info. I am using MSPetshop architecture.

In the SQL Server layer, I get the the data into a Data Model class:
if (reader.Read())
{
ShortTaskInfo item = new ShortTaskInfo();
item.TaskID = reader.GetInt32(0);
item.Subject = ( reader.IsDBNull(1) ? "" : reader.GetString(1) );
item.Details = ( reader.IsDBNull(2) ? "" : reader.GetString(2) );
item.DueDate = reader.GetDateTime(3);
}

But since I cannot pass back a null in DateTime, I am passing whatever I get
from the database. This field is optional, and can be null in database. I am
not able to handle these null values on my UI layer, and they are causing a
crash. I do not want to resort to workarrounds like passing
DateTime.MaxValue or DateTime.MinValue for null and checking for that on my
UI.


Well, you could create your own class, eg DateTimeWrapper, which would
store a DateTime inside it (potentially with conversions between the
two) - then you could have a null reference to a DateTimeWrapper
instead.

I'm still not sure exactly what you *are* doing though - what do you
want the final effect to be? As I said before, you definitely *don't*
have a null DateTime reference at the moment, as DateTime isn't a
reference type. I think you need to have a look at the stack trace from
the exception and work out exactly why you're getting it - that may
well give you a good idea about the best way to go about fixing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thanks, this should work.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Manish Jain <ma******@hotmail.com> wrote:
Sorry about the incomplete info. I am using MSPetshop architecture.

In the SQL Server layer, I get the the data into a Data Model class:
if (reader.Read())
{
ShortTaskInfo item = new ShortTaskInfo();
item.TaskID = reader.GetInt32(0);
item.Subject = ( reader.IsDBNull(1) ? "" : reader.GetString(1) );
item.Details = ( reader.IsDBNull(2) ? "" : reader.GetString(2) );
item.DueDate = reader.GetDateTime(3);
}

But since I cannot pass back a null in DateTime, I am passing whatever I get from the database. This field is optional, and can be null in database. I am not able to handle these null values on my UI layer, and they are causing a crash. I do not want to resort to workarrounds like passing
DateTime.MaxValue or DateTime.MinValue for null and checking for that on my UI.


Well, you could create your own class, eg DateTimeWrapper, which would
store a DateTime inside it (potentially with conversions between the
two) - then you could have a null reference to a DateTimeWrapper
instead.

I'm still not sure exactly what you *are* doing though - what do you
want the final effect to be? As I said before, you definitely *don't*
have a null DateTime reference at the moment, as DateTime isn't a
reference type. I think you need to have a look at the stack trace from
the exception and work out exactly why you're getting it - that may
well give you a good idea about the best way to go about fixing it.

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

Nov 15 '05 #5
Hi Manish,

Why don't you do the same with the DateTime column ? I mean if it's possible
that it's null on the DB then you should defenitly check it on your code,
now the BIG question is what to do in that case, I can think of two choises
: 1- Do as Jon said and write a wrapper class to handle it as you need in
your particular situation or 2- Set it to a particular value which can work
as a "null" value in your program ( maybe DateTime.Minvalue ) and later you
can check for it if ( item.DueDate == DateTime.MinValue ) // it was a null
on the DB.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Manish Jain" <ma******@hotmail.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
Sorry about the incomplete info. I am using MSPetshop architecture.

In the SQL Server layer, I get the the data into a Data Model class:
if (reader.Read())
{
ShortTaskInfo item = new ShortTaskInfo();
item.TaskID = reader.GetInt32(0);
item.Subject = ( reader.IsDBNull(1) ? "" : reader.GetString(1) );
item.Details = ( reader.IsDBNull(2) ? "" : reader.GetString(2) );
item.DueDate = reader.GetDateTime(3);
}

But since I cannot pass back a null in DateTime, I am passing whatever I get from the database. This field is optional, and can be null in database. I am not able to handle these null values on my UI layer, and they are causing a crash. I do not want to resort to workarrounds like passing
DateTime.MaxValue or DateTime.MinValue for null and checking for that on my UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Manish Jain <ma******@hotmail.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(DateTime date)
{
string text = date.ToString(); //This crashes if date is null
}


date can't be null. DateTime is a value type, null is a reference.
Please come up with a short but complete program to demonstrate your
problem - see http://www.pobox.com/~skeet/csharp/complete.html for
details.

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


Nov 15 '05 #6
I second the DateTime.MinValue concept. The only problem you need to
consider
though is in the GUI, you do not want to display DateTime.MinValue (1/1/01)
so you
have to handle the events like TextChanged, check for that and display a
blank.

I will never understand why MS didn't make DateTime an object class.

JIM
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Hi Manish,

Why don't you do the same with the DateTime column ? I mean if it's possible that it's null on the DB then you should defenitly check it on your code,
now the BIG question is what to do in that case, I can think of two choises : 1- Do as Jon said and write a wrapper class to handle it as you need in
your particular situation or 2- Set it to a particular value which can work as a "null" value in your program ( maybe DateTime.Minvalue ) and later you can check for it if ( item.DueDate == DateTime.MinValue ) // it was a null on the DB.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Manish Jain" <ma******@hotmail.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
Sorry about the incomplete info. I am using MSPetshop architecture.

In the SQL Server layer, I get the the data into a Data Model class:
if (reader.Read())
{
ShortTaskInfo item = new ShortTaskInfo();
item.TaskID = reader.GetInt32(0);
item.Subject = ( reader.IsDBNull(1) ? "" : reader.GetString(1) );
item.Details = ( reader.IsDBNull(2) ? "" : reader.GetString(2) );
item.DueDate = reader.GetDateTime(3);
}

But since I cannot pass back a null in DateTime, I am passing whatever I get
from the database. This field is optional, and can be null in database.

I am
not able to handle these null values on my UI layer, and they are
causing a
crash. I do not want to resort to workarrounds like passing
DateTime.MaxValue or DateTime.MinValue for null and checking for that on

my
UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Manish Jain <ma******@hotmail.com> wrote:
> Platform : ASP.Net/C#
>
> void SomeFunction(DateTime date)
> {
> string text = date.ToString(); //This crashes if date is null > }

date can't be null. DateTime is a value type, null is a reference.
Please come up with a short but complete program to demonstrate your
problem - see http://www.pobox.com/~skeet/csharp/complete.html for
details.

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



Nov 15 '05 #7
I'm working on a .NET library that implements a nullable
DateTime. It's open-source and it can be used in
commercial closed-source projects without restrictions
(MIT license).

The project is NullableTypes.
Look at http://nullabletypes.sourceforge.net/

HTH (luKa)
NullableTypes Project Manager

Nov 15 '05 #8
Luca Minudel writes:
The project is NullableTypes.
Look at http://nullabletypes.sourceforge.net/


Is that green-on-black nullable?

:)

Michael Roper
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brendan McLoughlin | last post: by
2 posts views Thread by Fred Nelson | last post: by
12 posts views Thread by conckrish | last post: by
4 posts views Thread by Lad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.