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

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 22 '05 #1
9 7636
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 22 '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 22 '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 22 '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 22 '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 22 '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 22 '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 22 '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 22 '05 #9
>
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 22 '05 #10

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

Similar topics

9
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
2
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
8
by: Manish Jain | last post by:
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...
6
by: Paulb1us | last post by:
I want to set a DateTime field to Null before passing it to the DB //First I check to see if anything is in this datarow column, because sometimes we have no data. DateTime dt; if (...
2
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
8
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
6
by: FatboyCanteen | last post by:
When I using dataset to append a null value to the datetime field. It throw a error -> can not convert db.null to system.date Can there is any standard to pass a Null value to the DateTime...
5
by: Kevin Yu | last post by:
hi all since the DateTime can't be assign null, the min value is set to 1901 or something, if in a application design, the date field can be null, so in between the UI and the DB, the business...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
4
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.