Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Assign a null value to DateTime property

Assign a null value to DateTime property

Scheduled Pinned Locked Moved WPF
questioncsharpdatabasewpfarchitecture
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxRazar
    wrote on last edited by
    #1

    I am developing a MVVM application using C#. I have a property called 'ReportedDate' of type DateTime which is optional in database field. In my view I have bound 'ReportedDate' property to txtReportedDate text box. After getting data from database, if there is no value in ReportedDate field, I want to display blank in txtReportedDate text box. But empty values cannot be assign to the property 'ReportedDate' in ViewModel. It gives errors. How can I do this?

    P J K 3 Replies Last reply
    0
    • M maxRazar

      I am developing a MVVM application using C#. I have a property called 'ReportedDate' of type DateTime which is optional in database field. In my view I have bound 'ReportedDate' property to txtReportedDate text box. After getting data from database, if there is no value in ReportedDate field, I want to display blank in txtReportedDate text box. But empty values cannot be assign to the property 'ReportedDate' in ViewModel. It gives errors. How can I do this?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      By itself, a DateTime cannot be null. To make it a nullable type, use DateTime?

      Chill _Maxxx_
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      M 1 Reply Last reply
      0
      • M maxRazar

        I am developing a MVVM application using C#. I have a property called 'ReportedDate' of type DateTime which is optional in database field. In my view I have bound 'ReportedDate' property to txtReportedDate text box. After getting data from database, if there is no value in ReportedDate field, I want to display blank in txtReportedDate text box. But empty values cannot be assign to the property 'ReportedDate' in ViewModel. It gives errors. How can I do this?

        J Offline
        J Offline
        Jason Gleim
        wrote on last edited by
        #3

        You might need a value converter as well to box and unbox the nullable DateTime. The framework will not directly cast DateTime? to DateTime.

        M 1 Reply Last reply
        0
        • P Pete OHanlon

          By itself, a DateTime cannot be null. To make it a nullable type, use DateTime?

          Chill _Maxxx_
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          M Offline
          M Offline
          maxRazar
          wrote on last edited by
          #4

          Could you please give an example?

          P 1 Reply Last reply
          0
          • J Jason Gleim

            You might need a value converter as well to box and unbox the nullable DateTime. The framework will not directly cast DateTime? to DateTime.

            M Offline
            M Offline
            maxRazar
            wrote on last edited by
            #5

            Could you please give an example?

            1 Reply Last reply
            0
            • M maxRazar

              Could you please give an example?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              I already showed you. The question mark after the type made it nullable.

              Chill _Maxxx_
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              1 Reply Last reply
              0
              • M maxRazar

                I am developing a MVVM application using C#. I have a property called 'ReportedDate' of type DateTime which is optional in database field. In my view I have bound 'ReportedDate' property to txtReportedDate text box. After getting data from database, if there is no value in ReportedDate field, I want to display blank in txtReportedDate text box. But empty values cannot be assign to the property 'ReportedDate' in ViewModel. It gives errors. How can I do this?

                K Offline
                K Offline
                koll Zhu
                wrote on last edited by
                #7

                why not choosing a nullable type or making a valueConvert when binding

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups