473,322 Members | 1,614 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,322 software developers and data experts.

How to represent "null" datetime ?

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 procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?

Nov 17 '05 #1
8 4305
You can't insert a null value into the database. But if the column is
nullable, what you *can* do is *not* insert a value into it. For example,
you can write a query or Stored Procedure that omits that column from an
INSERT statement.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

<cr************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?

Nov 17 '05 #2
SP
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
You can't insert a null value into the database.
Actually you can.

But if the column is nullable, what you *can* do is *not* insert a value into it. For example,
you can write a query or Stored Procedure that omits that column from an
INSERT statement.
How many stored procedures would you need to write for a table with 10 date
fields using that logic?!!!.

Write one stored procedure and if the DateTime value is MinValue then pass
System.DBNull.Value as the value for the parameter.

SP

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.

<cr************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?


Nov 17 '05 #3
My apologies. Haven't been working too closely with databases for the most
part lately, and have fallen behind in my knowledge. How embarrassing!

You are, of course, correct. I shall go and scourge myself immediately!

--
:-(,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"SP" <ec***********@hotmail.com> wrote in message
news:OM**************@tk2msftngp13.phx.gbl...
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
You can't insert a null value into the database.


Actually you can.

But if the column is
nullable, what you *can* do is *not* insert a value into it. For example,
you can write a query or Stored Procedure that omits that column from an
INSERT statement.


How many stored procedures would you need to write for a table with 10
date fields using that logic?!!!.

Write one stored procedure and if the DateTime value is MinValue then pass
System.DBNull.Value as the value for the parameter.

SP

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.

<cr************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?



Nov 17 '05 #4
Pre-2.0, you can use DateTime.MinValue or DateTime.MaxValue, depending
upon the situation. For something like a null expiry date (not expired
yet) you would use DateTime.MaxValue. In fact, I tend to use
DateTime.MaxValue much more, as it indicates "not yet", which is
typically what a null date and time means. For null date / times that
indicate "missing information" I imagine that DateTime.MinValue would
be more appropriate.

Starting in C# 2.0 (due out shortly), you will be able to specify the
parameters to your generic method as

DateTime? createDateTime

The question mark indicates a nullable value type.

Nov 17 '05 #5
One thing though, about the DateTime.MinValue that's so much fun, is that
the MinValue is outside the DateTimePickers control's legal bounds for a
date... yay! So be careful about binding your dates blindly to the UI
control.

What I would do is use 1900/1/1 instead... store it as a const somewhere and
test for that, instead of the MinValue.
This only applies if you have any GUI related stuff here, and TBH, it
doesn't sound like it applies in your case. Just thought I'd add my 2 bits.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
My apologies. Haven't been working too closely with databases for the most
part lately, and have fallen behind in my knowledge. How embarrassing!

You are, of course, correct. I shall go and scourge myself immediately!

--
:-(,

Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.

"SP" <ec***********@hotmail.com> wrote in message
news:OM**************@tk2msftngp13.phx.gbl...
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
You can't insert a null value into the database.


Actually you can.

But if the column is
nullable, what you *can* do is *not* insert a value into it. For
example, you can write a query or Stored Procedure that omits that
column from an INSERT statement.


How many stored procedures would you need to write for a table with 10
date fields using that logic?!!!.

Write one stored procedure and if the DateTime value is MinValue then
pass System.DBNull.Value as the value for the parameter.

SP

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.

<cr************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 procedure and must save
either a null or a real date.

However, passing a hardcoded "null" to call this function causes a
compilation error :

cx.cs(136): Argument '16': cannot convert from '<null>' to
'System.DateTime'

So, the question is how one should handle null dates ? What's the
common approach ?
May be use the datetime.minvalue as a meaning of "null" ?



Nov 17 '05 #6
Oh, yes: no promises about whether the version 2.0 DateTimePicker will
be able to handle DateTime? properly. I haven't heard one way or
another. Anyone already using 2.0 care to comment?

Nov 17 '05 #7
A simple and safe approach is to write separate methods (or overloaded
ones) to handle such special cases. If that ends up in too many
overloaded methods, how about writting a wrapper class for DateTime
structure?

Regards,
Thi - http://thith.blogspot.com

Nov 17 '05 #8

"John Richardson" <j3**********@hotmail.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
One thing though, about the DateTime.MinValue that's so much fun, is that
the MinValue is outside the DateTimePickers control's legal bounds for a
date... yay! So be careful about binding your dates blindly to the UI
control.

If it were me, I'd stick to DateTime.MinValue, since (in your case) it acts
like a -null- value for the UI. That is, you always have to be careful
about binding null values, and well, the same would be true for the
DateTime.MinValue.

Scott
Nov 17 '05 #9

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

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
10
by: dcrespo | last post by:
Hi all, How can I replace all None values with the string 'Null' in a dictionary? For example: convert this: a = {'item1': 45, 'item2': None} into this:
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
4
by: Peter Hemmingsen | last post by:
Hi, I have a dotnet object (implemented in mc++ and used in c#) which have a property called "Info". The Info property is also a dotnet object (implemented in mc++). In the constructor of the...
9
by: D. Shane Fowlkes | last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field...
5
by: rengeek33 | last post by:
I am building a SQL statement for Oracle and need one part of it to read: , myvar = null, myvar2 = "1", myvar3 = "0", myvar4 = null, etc. I am constructing this string using the String...
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.