473,513 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set DateTime value to null

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
Nov 16 '05 #1
11 27917
news.microsoft.com <ru*********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
news.microsoft.com <ru*********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
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
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
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" <ru*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
news.microsoft.com <ru*********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #5
Cor Ligthert <no**********@planet.nl> wrote:
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#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
You can set it to new DateTime(0) which is should also be semantically
identical to DateTime.Minvalue.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"news.microsoft.com" <ru*********@hotmail.com> schrieb im Newsbeitrag
news:uB**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #7
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


"cody" <no****************@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can set it to new DateTime(0) which is should also be semantically
identical to DateTime.Minvalue.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"news.microsoft.com" <ru*********@hotmail.com> schrieb im Newsbeitrag
news:uB**************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #8
> 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.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #9
> 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.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #10
>
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
Nov 16 '05 #11
>
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
Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
548
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
4
8225
by: Frank Meng | last post by:
Hi. I want to save a DateTime value with format like "2004-02-11T12:11:17.0000000-05:00" using XmlTextWriter. I think I should write codes something like: StreamWriter sw=new...
1
3085
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
1
4943
by: Matt Jensen | last post by:
Howdy all I know this is a common question as I've searched a lot of this NG on the topic, however I haven't been able to find an answer. All I want to do is pass a DateTime value to a SQL Server...
1
5459
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
4
8660
by: Arpan | last post by:
A SQL Server 2005 DB table has 4 columns namely "ID" (IDENTITY int column), "FirstName" (varchar(50)), "LastName" (varchar(50)) & "DOB" (datetime). Now when I am trying to add a new row using the...
7
1675
by: Sala | last post by:
Hi experts! If system time 12:00 am my application variable value will be null.. I am trying to get null value of application but that time i'll make some actions to that page.. So pls tell me how...
15
2583
by: mayurtandel | last post by:
i have problem whith store datetime value to database table the my system date formate is dd/mm/yyyy the date is store in mm/dd/yyyy formate what is the problem my code is as follow Public...
10
14195
by: Newbie | last post by:
howdy... i am trying to execute a statement where i insert a record with datetime values into a sql database. Dim sqlcmd As New SqlCommand sqlcmd.CommandText = "update tbl_event set...
2
4458
by: raam | last post by:
create proc getproviders as begin select ProviderType,ProviderTypeId,ProviderName,City,State,Country, ...
0
7264
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
7386
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
7534
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
5689
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,...
1
5094
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...
0
3236
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...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
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 ...
0
459
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...

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.