Since DateTime is a value type (like int), a Null value cannot be assigned to it. In this instance many people use DateTime.MinValue or DateTime.MaxValue instead.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
This must be my picky day ;-) The example does not work. 'object' is a reserved word, the name used in the sample is _myDateTime, so it should be:
if (_myDateTime != null) ...
or
if (_myDateTime.HasValue) ...
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
Please mark this post as Answer if it is of help to you!
when you stop trying, you’ve stopped succeeding.” The joy is in the trying — knowing that you might fail sometime. If you want to succeed, you need to keep stretching yourself.
Please mark this post as Answer if it is of help to you!
when you stop trying, you’ve stopped succeeding.” The joy is in the trying — knowing that you might fail sometime. If you want to succeed, you need to keep stretching yourself.
vasanth.kuma...
Contributor
5324 Points
1041 Posts
assigning Null value to datetime in C#
Sep 20, 2007 10:41 AM|LINK
Hi,
How can I assign Null value to datetime?
C#
Software Engineer.
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: assigning Null value to datetime in C#
Sep 20, 2007 12:25 PM|LINK
Since DateTime is a value type (like int), a Null value cannot be assigned to it. In this instance many people use DateTime.MinValue or DateTime.MaxValue instead.
Microsoft MVP - ASP.NET
n.ocampo
Member
180 Points
36 Posts
Re: assigning Null value to datetime in C#
Sep 20, 2007 12:41 PM|LINK
You can use Nullable Types to achieve that.
DateTime? dateSample;
dateSample.Value = null;
//or
Nullable<DateTime> dateSample2;
dateSample2.Value = null;
Svante
All-Star
18347 Points
2300 Posts
Re: assigning Null value to datetime in C#
Sep 20, 2007 01:01 PM|LINK
Nullable types is correct, but your example is not :-( You cannot assign null to the Value property, it is of type 'DateTime'.
DateTime? dateSample; dateSample = null; //or Nullable dateSample2; dateSample2 = null;AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
iainpb
Participant
1173 Points
250 Posts
Re: assigning Null value to datetime in C#
Sep 20, 2007 02:38 PM|LINK
Nullable<DateTime> _myDateTime;
will work, you should be able to set it to null
Check for nulls:
if (object.myDateTime != null)
or
if (object.myDateTime.HasValue)
hope this helps
nullable DateTime .net 2.0 time date
Mark helpful posts as "Answer"
Check out my cheese making blog
http://cheeseathome.blogspot.com
Svante
All-Star
18347 Points
2300 Posts
Re: assigning Null value to datetime in C#
Sep 20, 2007 02:52 PM|LINK
This must be my picky day ;-) The example does not work. 'object' is a reserved word, the name used in the sample is _myDateTime, so it should be:
if (_myDateTime != null) ...
or
if (_myDateTime.HasValue) ...
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
iainpb
Participant
1173 Points
250 Posts
Re: assigning Null value to datetime in C#
Sep 20, 2007 03:09 PM|LINK
<post deleted>
Mark helpful posts as "Answer"
Check out my cheese making blog
http://cheeseathome.blogspot.com
Suresh Thaku...
Member
30 Points
14 Posts
Re: assigning Null value to datetime in C#
Dec 29, 2009 05:21 AM|LINK
m_Date = _Date != null ? _Date : (Nullable<DateTime>)null;
when you stop trying, you’ve stopped succeeding.” The joy is in the trying — knowing that you might fail sometime. If you want to succeed, you need to keep stretching yourself.
Suresh Thaku...
Member
30 Points
14 Posts
Re: assigning Null value to datetime in C#
Dec 29, 2009 06:26 AM|LINK
You can also write code like
m_Date = dr.IsDBNull(dr.GetOrdinal("_Date")) ? (DateTime?)null : (DateTime?)Convert.ToDateTime(dr["_Date"]);
when you stop trying, you’ve stopped succeeding.” The joy is in the trying — knowing that you might fail sometime. If you want to succeed, you need to keep stretching yourself.
gopalchettri
Member
39 Points
39 Posts
Re: assigning Null value to datetime in C#
May 31, 2010 08:13 AM|LINK
you need to assign like this in C#
DateTime? variablename = null;
GopalChettri
(MCP)