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. Visual Basic
  4. Trouble with recover a date from a datetimepicker

Trouble with recover a date from a datetimepicker

Scheduled Pinned Locked Moved Visual Basic
databasehelpc++sql-servercom
6 Posts 2 Posters 1 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
    Member 11220465
    wrote on last edited by
    #1

    Hey guys, I hope that somebody here can help me ... I have on my SQL SERVER 2014 DATABASE a table with a field Datetime Null My date appears like 2019-05-15 00:00:00.000 When I filter with a stored procedure works fine using the params: cmd = New SqlCommand("SELECT vta.datesale FROM Sales WHERE vta.datesale = @mydate", myConn)

    cmd.Parameters.AddWithValue("@mydate", Now.Date())

    but my trouble is when: 1.- I use the same cmd cmd = New SqlCommand("SELECT vta.datesale FROM Sales WHERE vta.datesale = @mydate", myConn) 2.- I have a DateTimePicker and I choose any day 3.- My code in the event ValueChanged() of my DateTimePicker is: a.- The same cmd in the number 1 b.- then ... cmd.Parameters.AddWithValue("@mydate", dtMyDate.Text) dtMyDate = Control DateTimePicker. I'm receiving this error: "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value" Somebody can help me?. Thanks in advance and excuse my grammar; I'm not a native speaker and I don't like to use translators. Javier.-

    L 1 Reply Last reply
    0
    • M Member 11220465

      Hey guys, I hope that somebody here can help me ... I have on my SQL SERVER 2014 DATABASE a table with a field Datetime Null My date appears like 2019-05-15 00:00:00.000 When I filter with a stored procedure works fine using the params: cmd = New SqlCommand("SELECT vta.datesale FROM Sales WHERE vta.datesale = @mydate", myConn)

      cmd.Parameters.AddWithValue("@mydate", Now.Date())

      but my trouble is when: 1.- I use the same cmd cmd = New SqlCommand("SELECT vta.datesale FROM Sales WHERE vta.datesale = @mydate", myConn) 2.- I have a DateTimePicker and I choose any day 3.- My code in the event ValueChanged() of my DateTimePicker is: a.- The same cmd in the number 1 b.- then ... cmd.Parameters.AddWithValue("@mydate", dtMyDate.Text) dtMyDate = Control DateTimePicker. I'm receiving this error: "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value" Somebody can help me?. Thanks in advance and excuse my grammar; I'm not a native speaker and I don't like to use translators. Javier.-

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 11220465 wrote:

      "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

      Means the runtime tries to convert your datetime from a varchar, or string. You don't want that, if you're supplying DateTime, it should be interpreted as such. Show us your code, we'll be gentle.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      M 1 Reply Last reply
      0
      • L Lost User

        Member 11220465 wrote:

        "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

        Means the runtime tries to convert your datetime from a varchar, or string. You don't want that, if you're supplying DateTime, it should be interpreted as such. Show us your code, we'll be gentle.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        M Offline
        M Offline
        Member 11220465
        wrote on last edited by
        #3

        Thanks a lot for your time, Eddy .... I'll share you (in Spanish) my screenshots 1.- Data storaged on my table Ventas https://ibb.co/TBmHSm6 You can see: yyyy-mm-dd hh:mm:ss.sss .... The field is DateTime (accepting NULLS, before with no accept NULLs I had the same error). 2.- Code from the event ValueChanged() of my control DateTimePicker (dtFecha)... https://ibb.co/pbsqnqP 3.- Error that I received when I try to access a day from my DateTimePicker that has data. https://ibb.co/pQYTfQF Thanks a lot, gentleman or any other bud that can help me. And again, excuse my grammar mistakes; I'm not a native speaker. Thanks guys, Javier.-

        L 1 Reply Last reply
        0
        • M Member 11220465

          Thanks a lot for your time, Eddy .... I'll share you (in Spanish) my screenshots 1.- Data storaged on my table Ventas https://ibb.co/TBmHSm6 You can see: yyyy-mm-dd hh:mm:ss.sss .... The field is DateTime (accepting NULLS, before with no accept NULLs I had the same error). 2.- Code from the event ValueChanged() of my control DateTimePicker (dtFecha)... https://ibb.co/pbsqnqP 3.- Error that I received when I try to access a day from my DateTimePicker that has data. https://ibb.co/pQYTfQF Thanks a lot, gentleman or any other bud that can help me. And again, excuse my grammar mistakes; I'm not a native speaker. Thanks guys, Javier.-

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 11220465 wrote:

          And again, excuse my grammar mistakes; I'm not a native speaker.

          Your English is fine. You are fetching the .Text property of the DataTimePicker, which returns a string. Then the code will try to turn that (locally formatted) string and convert it back to a DateTime, which then fails. Try dtFecha.Value instead, it should give you a DateTime value, not a formatted string.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          M 1 Reply Last reply
          0
          • L Lost User

            Member 11220465 wrote:

            And again, excuse my grammar mistakes; I'm not a native speaker.

            Your English is fine. You are fetching the .Text property of the DataTimePicker, which returns a string. Then the code will try to turn that (locally formatted) string and convert it back to a DateTime, which then fails. Try dtFecha.Value instead, it should give you a DateTime value, not a formatted string.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            M Offline
            M Offline
            Member 11220465
            wrote on last edited by
            #5

            You're awesome :-) Thanks a lot, Eddy .... You gave me the real clue in here ... "Then the code will try to turn that (locally formatted) string and convert it back to a DateTime, which then fails." When I read that, I used a MsgBox (to test) and you were right, dude :-) I tried with dtFecha.Value but failed .... but I picked another way: CDate(dtFecha.Text) and it's working fine :-) You guy, you're the real MVP hahaha :-) Thanks a lot, bud. A great hug and my best wishes for you and your family, my man :-) :cool::cool::cool::thumbsup::thumbsup::thumbsup::thumbsup:

            L 1 Reply Last reply
            0
            • M Member 11220465

              You're awesome :-) Thanks a lot, Eddy .... You gave me the real clue in here ... "Then the code will try to turn that (locally formatted) string and convert it back to a DateTime, which then fails." When I read that, I used a MsgBox (to test) and you were right, dude :-) I tried with dtFecha.Value but failed .... but I picked another way: CDate(dtFecha.Text) and it's working fine :-) You guy, you're the real MVP hahaha :-) Thanks a lot, bud. A great hug and my best wishes for you and your family, my man :-) :cool::cool::cool::thumbsup::thumbsup::thumbsup::thumbsup:

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Glad I could help :)

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              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