Datetime Nullable objects must have a value ? RRS feed

  • Question

  • Hi in database i have Table1,
    it has 2 fields, prevDay, currDay.

    sometime the fields are null in database, not filled in yet.

    in the c# code i want to check if null or not 1st,

      if ((DateTime)Table1.prevDay != (DateTime?)null) 

    when i run it, it says Nullable objects must have a value.
    what's the correct way ?

    also what's the difference between DateTime and DateTime?   

    thanks !
    Wednesday, August 21, 2013 8:10 PM


  • Hi ningji;

    To test Nullable types use the HasValue which is a bool like this.

    if ( Table1.prevDay.HasValue  ) 

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Fred Bao Thursday, August 22, 2013 6:33 AM
    • Marked as answer by ningji Friday, August 23, 2013 5:14 PM
    Wednesday, August 21, 2013 8:35 PM