Set DateTime value to null

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hello,



I have DateTime property.

How can I set this value to null?



I try DBNull.Value but it doesn't work.





Thanks a lot,

Ruslan
 
news.microsoft.com said:
I have DateTime property.

How can I set this value to null?

You can't - just like you can't set an int property to null, or any
other value type property to null. null is a reference, and is
incompatible with value types.
 
Oops, I thought what DateTime is a reference type. So if I need to assign a
null value to a parameter for stored procedure I have to write:

command.Parameters.Add("@declaration_date", SqlDbType.DateTime).Value =
DBNull.Value;



or there is another way?



Thanks



Ruslan
 
Hi Jon,

When it is a real property you are right, however the date value is
something special what takes time for me in C# to be sure to give the right
answer.

So I did not answer this message, can you when I show you this in VBNet

as a property or value
mydate = nothing (is ISO 1900/01/01)

dr("mydate") = dbnull.value

Because this is answered from the ADONET newsgroup I assume that is meant
with this question the last. From which I assume it is (almost) exactly the
same in C#, however before I make a mistake.

Cor
 
You can just use DBNull.Value to the parameter.

Add("@declaration_date", DBNull.Value);

The problem is you have to determine to do that beforehand since the .NET
(C#) DateTime can't be null itself. You have the same problem in reverse
when you read a null from the database. I've seen one example of wrapping
the DateTime class and using DateTime.MinValue as a way to represent a null
database value, since that's a date that's never going to be used for most
applications.

news.microsoft.com said:
Oops, I thought what DateTime is a reference type. So if I need to assign a
null value to a parameter for stored procedure I have to write:

command.Parameters.Add("@declaration_date", SqlDbType.DateTime).Value =
DBNull.Value;



or there is another way?



Thanks



Ruslan
 
Cor Ligthert said:
When it is a real property you are right, however the date value is
something special what takes time for me in C# to be sure to give the right
answer.

So I did not answer this message, can you when I show you this in VBNet

as a property or value
mydate = nothing (is ISO 1900/01/01)

Exactly - "Nothing" in VB.NET isn't the same as null. For value types,
it's just the default value for the type (the "zero" value to some
extent). The problem is that it's not the same as a null value - it's a
perfectly good date in itself, and one which may need to be stored in a
database.
dr("mydate") = dbnull.value

*That's* a real null value.
Because this is answered from the ADONET newsgroup I assume that is meant
with this question the last.

Well, the use of the word "property" is the problem here. If the OP is
using a strongly typed dataset, he could well be dealing with a real
property.
From which I assume it is (almost) exactly the
same in C#, however before I make a mistake.

Yup, you'd use

dr["mydate"] = DBNull.Value;

in C#.
 
You can set it to new DateTime(0) which is should also be semantically
identical to DateTime.Minvalue.
 
Well, in VB.Net, we have a .Net datepicker control we wrote. The control is
complete but has one bug. We can't set the value to Nothing because when we
display the value (as a string) it always returns the 0 value of the date. How
can we improvise without having the user to check for the 0 value before
retrieving the string? Basically, we don't want to check for the 0 value
because, like someone else said here, the 0 value is a perfectly legal and
possibly needed value that the user selected.

Thanks for any help :)

Mythran
 
Well, in VB.Net, we have a .Net datepicker control we wrote. The control
is
complete but has one bug. We can't set the value to Nothing because when we
display the value (as a string) it always returns the 0 value of the date. How
can we improvise without having the user to check for the 0 value before
retrieving the string? Basically, we don't want to check for the 0 value
because, like someone else said here, the 0 value is a perfectly legal and
possibly needed value that the user selected.


Sure 0 is theoretically a valid date: it is IIRC "00001-01-01 00:00" but no
one would select such a date you can easily forbid it. for almost all
applications only years from 1850 will be useful anyway.
Even the windows datapicker does ot allow a 0 date.
 
Sure 0 is theoretically a valid date: it is IIRC "00001-01-01 00:00" but no
one would select such a date you can easily forbid it. for almost all
applications only years from 1850 will be useful anyway.
Even the windows datapicker does ot allow a 0 date.
It is terrible that you have to Add 2000 when it is before 1800 and subtract
it afterwards, because it starts at 1800 however you can go on until 9999,
the one who has invented this was a real optimist.

:-)

Cor
 
Back
Top