Results 1 to 8 of 8

Thread: [RESOLVED] 'System.DateTime' and 'System.DBNull'

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 'System.DateTime' and 'System.DBNull'

    Please post your C# questions in the C# forum. I've asked the mods to move this thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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 )

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. cmd.Parameters["@p_order_date"].Value = (this.OrderDate == DateTime.MaxValue)
    2.                                              ? DBNull.Value
    3.                                              : (object) this.OrderDate;
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Re: 'System.DateTime' and 'System.DBNull'

    It works!

    Thank you jmcilhinney

    Paul

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width