|
-
Jan 20th, 2011, 02:46 PM
#1
Thread Starter
Junior Member
[RESOLVED] 'System.DateTime' and 'System.DBNull'
Hello,
I'm trying to send a Null value in a DateTime field while calling my stored procedure.
If I use this first method (short with one line), i got the following error message:
cmd.Parameters["@p_order_date"].Value = (this.OrderDate == DateTime.MaxValue) ? DBNull.Value : this.OrderDate;
Error Message:
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull'
However, it works correctly if I use the 2nd method:
if (this.OrderDate == DateTime.MaxValue)
{
cmd.Parameters["@p_order_date"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@p_order_date"].Value = this.OrderDate;
}
Can you help me to fix the problem with the first method ?
Thank you
Paul
Last edited by paulnamroud; Jan 20th, 2011 at 02:52 PM.
-
Jan 20th, 2011, 02:50 PM
#2
Re: 'System.DateTime' and 'System.DBNull'
I'm pretty sure you can just use 'null' instead of DBNull.Value. You will always get a DBNull back from the database (when loading), but you should be able to save it as null.
Specifically I think the error is that the 'true' and 'false' return values of the ternary operator must be of the same type (at least that's what I think, I didn't check it). DBNull.Value and DateTime are not the same type (and don't inherit a common base type) so you can't do that. If you use null however then the ternary operator can return a 'DateTime?' (nullable DateTime) type which fits both.
Also, this is the VB forum, not the C# forum.
-
Jan 20th, 2011, 06:07 PM
#3
Re: 'System.DateTime' and 'System.DBNull'
Please post your C# questions in the C# forum. I've asked the mods to move this thread.
-
Jan 20th, 2011, 06:15 PM
#4
Re: 'System.DateTime' and 'System.DBNull'
Thread moved from the 'VB.Net' forum to the 'C#' forum
(thanks as always for letting us know jmcilhinney )
-
Jan 20th, 2011, 07:08 PM
#5
Thread Starter
Junior Member
Re: 'System.DateTime' and 'System.DBNull'
Thank you Nick for your post ...
You might be right ...
But how come it works with the second method without any error message ?
By the way, I got the the error message while compiling my project!
Thank you,
Paul
-
Jan 20th, 2011, 07:09 PM
#6
Re: 'System.DateTime' and 'System.DBNull'
The ternary operator does indeed require that the two possible return values can be interpreted as a common type. In this case, the simple option is to cast the DateTime as type Object. The DBNull.Value will then also be intepreted as Object. Because you're assigning to a property of type Object, that's fine.
csharp Code:
cmd.Parameters["@p_order_date"].Value = (this.OrderDate == DateTime.MaxValue) ? DBNull.Value : (object) this.OrderDate;
-
Jan 26th, 2011, 12:35 PM
#7
Thread Starter
Junior Member
Re: 'System.DateTime' and 'System.DBNull'
It works!
Thank you jmcilhinney
Paul
-
Jan 26th, 2011, 01:49 PM
#8
Re: 'System.DateTime' and 'System.DBNull'
As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
(this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)
You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|