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. C#
  4. Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'

Scheduled Pinned Locked Moved C#
databasehelpcsharpquestion
10 Posts 5 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.
  • L Offline
    L Offline
    Lodeclaw
    wrote on last edited by
    #1

    DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

    I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!

    K A B 3 Replies Last reply
    0
    • L Lodeclaw

      DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

      I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      Lodeclaw wrote:

      System.DateTime?

      What is System.DateTime**?** ? I have only heard of System.DateTime. Try removing the "?".

      Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

      HAI
      CAN HAS STDIO?
      PLZ OPEN FILE "SIGNATURE.TXT"?
      AWSUM THX
      VISIBLE FILE
      O NOES
      INVISIBLE "ERROR!"
      KTHXBYE

      L C 2 Replies Last reply
      0
      • K Kristian Sixhoj

        Lodeclaw wrote:

        System.DateTime?

        What is System.DateTime**?** ? I have only heard of System.DateTime. Try removing the "?".

        Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

        HAI
        CAN HAS STDIO?
        PLZ OPEN FILE "SIGNATURE.TXT"?
        AWSUM THX
        VISIBLE FILE
        O NOES
        INVISIBLE "ERROR!"
        KTHXBYE

        L Offline
        L Offline
        Lodeclaw
        wrote on last edited by
        #3

        That's confusing me, as well. I didn't type System.DateTime? anywhere. If I had typed it incorrectly the debugger would have notified me when I typed it. I'll do a search for it, anyway.

        1 Reply Last reply
        0
        • L Lodeclaw

          DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

          I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Look closely at the message. DateTime? is a nullable DateTime. You'll have to do the explicit conversion [EDIT] or alternatively declare you date rented as the nullable DateTime? type. Alan.

          L 1 Reply Last reply
          0
          • A Alan N

            Look closely at the message. DateTime? is a nullable DateTime. You'll have to do the explicit conversion [EDIT] or alternatively declare you date rented as the nullable DateTime? type. Alan.

            L Offline
            L Offline
            Lodeclaw
            wrote on last edited by
            #5

            I've been trying to do the explicit conversion. How do I know what the explicit conversion is? I've tried DateTime.Parse the query results as a string and I've tried Convert.DateTime. Can you offer any other possibilities?

            1 Reply Last reply
            0
            • L Lodeclaw

              DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

              I can't figure out why I'm getting this exception thrown by this code. I'm trying to collect a single cell from the DateTime column "Time_Rented" using a SQL statement. The SQL statement works fine in the query builder, so it has to be something in the C# code. The entire error message is: "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists ( are you missing a case?)" Both sides of the statement are DateTime, aren't they? I don't understand why it can't convert it. Please help!

              B Offline
              B Offline
              Ben Fair
              wrote on last edited by
              #6

              DateTime? is short-hand for Nullable<DateTime>, using the Nullable<T> class introduced in .NET Framework 2.0. The Nullable types are actually a class that is a wrapper around a value type. To get the underlying value from a Nullable<T>, use the Value property like so:

              DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text)).Value;

              Or you can declare dateRented as Nullable<DateTime> (same as DateTime?) rather than a normal DateTime, like so:

              DateTime? dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

              OR

              Nullable<DateTime> dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

              The message really means that the getTimeRentedbyInvoiceNo() function is returning a DateTime? and you are trying to stuff that return value into a DateTime; and they are two different types.

              Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.

              L 1 Reply Last reply
              0
              • B Ben Fair

                DateTime? is short-hand for Nullable<DateTime>, using the Nullable<T> class introduced in .NET Framework 2.0. The Nullable types are actually a class that is a wrapper around a value type. To get the underlying value from a Nullable<T>, use the Value property like so:

                DateTime dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text)).Value;

                Or you can declare dateRented as Nullable<DateTime> (same as DateTime?) rather than a normal DateTime, like so:

                DateTime? dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

                OR

                Nullable<DateTime> dateRented = rental_InvoiceTableAdapter1.getTimeRentedbyInvoiceNo(int.Parse(txtInvoiceNo.Text));

                The message really means that the getTimeRentedbyInvoiceNo() function is returning a DateTime? and you are trying to stuff that return value into a DateTime; and they are two different types.

                Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.

                L Offline
                L Offline
                Lodeclaw
                wrote on last edited by
                #7

                I tried those and even though the debugger is ok with them, and the other conversions I've tried, I get an exception telling me the input value is not in the correct format.

                A 1 Reply Last reply
                0
                • L Lodeclaw

                  I tried those and even though the debugger is ok with them, and the other conversions I've tried, I get an exception telling me the input value is not in the correct format.

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #8

                  Lodeclaw wrote:

                  I get an exception telling me the input value is not in the correct format.

                  This may be a different problem now. Where did the exception arise and do you know that the result of int.Parse is valid input for your function? Alan.

                  L 1 Reply Last reply
                  0
                  • A Alan N

                    Lodeclaw wrote:

                    I get an exception telling me the input value is not in the correct format.

                    This may be a different problem now. Where did the exception arise and do you know that the result of int.Parse is valid input for your function? Alan.

                    L Offline
                    L Offline
                    Lodeclaw
                    wrote on last edited by
                    #9

                    For some reason the query that fills the txtInvoiceNo textbox is not yielding a result, so my input is an empty string. I'll have to figure out why that is. In any case, this problem is solved, then. Thanks everyone for helping this poor noob. :doh: I learned some new things.

                    1 Reply Last reply
                    0
                    • K Kristian Sixhoj

                      Lodeclaw wrote:

                      System.DateTime?

                      What is System.DateTime**?** ? I have only heard of System.DateTime. Try removing the "?".

                      Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

                      HAI
                      CAN HAS STDIO?
                      PLZ OPEN FILE "SIGNATURE.TXT"?
                      AWSUM THX
                      VISIBLE FILE
                      O NOES
                      INVISIBLE "ERROR!"
                      KTHXBYE

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      It's a nullable datetime. int? is a nullable int, for example.

                      Christian Graus Driven to the arms of OSX by Vista.

                      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