473,785 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling null DateTime objects?

Platform : ASP.Net/C#

void SomeFunction(Da teTime 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 19248
Manish Jain <ma******@hotma il.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(Da teTime 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.co m>
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.GetStrin g(1) );
item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
item.DueDate = reader.GetDateT ime(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.MaxVal ue or DateTime.MinVal ue for null and checking for that on my
UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Manish Jain <ma******@hotma il.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(Da teTime 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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
Manish Jain <ma******@hotma il.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.GetStrin g(1) );
item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
item.DueDate = reader.GetDateT ime(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.MaxVal ue or DateTime.MinVal ue 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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Manish Jain <ma******@hotma il.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.GetStrin g(1) );
item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
item.DueDate = reader.GetDateT ime(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.MaxVal ue or DateTime.MinVal ue 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.co m>
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.Minval ue ) and later you
can check for it if ( item.DueDate == DateTime.MinVal ue ) // 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******@hotma il.com> wrote in message
news:eE******** ******@TK2MSFTN GP09.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.GetStrin g(1) );
item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
item.DueDate = reader.GetDateT ime(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.MaxVal ue or DateTime.MinVal ue for null and checking for that on my UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Manish Jain <ma******@hotma il.com> wrote:
Platform : ASP.Net/C#

void SomeFunction(Da teTime 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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #6
I second the DateTime.MinVal ue concept. The only problem you need to
consider
though is in the GUI, you do not want to display DateTime.MinVal ue (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.mach in AT dot.state.fl.us > wrote in message
news:OG******** ******@tk2msftn gp13.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.Minval ue ) and later you can check for it if ( item.DueDate == DateTime.MinVal ue ) // 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******@hotma il.com> wrote in message
news:eE******** ******@TK2MSFTN GP09.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.GetStrin g(1) );
item.Details = ( reader.IsDBNull (2) ? "" : reader.GetStrin g(2) );
item.DueDate = reader.GetDateT ime(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.MaxVal ue or DateTime.MinVal ue for null and checking for that on

my
UI.

Hope I am more clear this time.

Manish

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Manish Jain <ma******@hotma il.com> wrote:
> Platform : ASP.Net/C#
>
> void SomeFunction(Da teTime 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.co m>
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
2892
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) >>> mar2003 = mx.DateTime.Date(2003, 3) >>> alldates = mk_list(startdate=nov1999, enddate=mar2003)
4
332
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 from the DB table columns, standard enough. But what about the instance where a column could be null.
2
17713
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
9471
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
61221
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 tell me how to compare ??? Thanx.. Krish
4
1927
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, 765000) Thank you for help L.
1
31396
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. I then want to pass the value of this text box as a datetime variable to my stored procedure which inserts or updates the row. As I mentioned above, the textbox may be left blank - hence I would want to pass a null to the stored proc. How do I...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.