473,405 Members | 2,334 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,405 software developers and data experts.

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 19208
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: python | last post by:
Hi- I want to make a list of mx.DateTime objects that cover all the monthly intervals between two points. For example: >>> import mx.DateTime >>> nov1999 = mx.DateTime.Date(1999, 11) >>>...
4
by: Brendan McLoughlin | last post by:
Hi, I am looking for opinions and alternatives for handling null values in a data object which reads a record from a database table. This object will have properties which will be populated...
2
by: Fred Nelson | last post by:
Hi: Another C# newby question: How do I declare a null DateTime variable: DateTime myDate = null; I get the error:
1
by: Ron | last post by:
How can I find the difference in seconds of two DateTime objects. I have looked at the Subtract method but this gives me back another DateTime object. Thanks, Ron
12
by: conckrish | last post by:
Hi all.. Can anyone tell me how to compare datetime objects?I ve three objects namely Current date,start date and end date.. I need to check the current date with Start date and end date....Plz...
4
by: Lad | last post by:
How can I find days and minutes difference between two datetime objects? For example If I have b=datetime.datetime(2006, 8, 2, 8, 57, 28, 687000) a=datetime.datetime(2006, 8, 1, 18, 19, 45,...
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.