Handling null DateTime objects?

  • Thread starter Thread starter Manish Jain
  • Start date Start date
M

Manish Jain

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
 
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
 
Manish Jain said:
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.
 
Thanks, this should work.

Jon Skeet said:
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.
 
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 said:
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
 
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 said:
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 said:
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
 
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
 
Back
Top