Trouble with recover a date from a datetimepicker
-
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.-
-
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.-
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.
-
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.
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.-
-
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.-
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 theDataTimePicker
, which returns a string. Then the code will try to turn that (locally formatted) string and convert it back to aDateTime
, which then fails. TrydtFecha.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.
-
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 theDataTimePicker
, which returns a string. Then the code will try to turn that (locally formatted) string and convert it back to aDateTime
, which then fails. TrydtFecha.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.
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:
-
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: