none
Which is better data type for date time picker format string"dd/MM/yyy hh:mm:ss:tt" RRS feed

  • Question

  • Hi Everybody,

    I have used date time picker with a custom format "dd/MM/yyy hh:mm:ss:tt" and save it in the access database by using this code

    With cmd.Parameters
                    .Add("@DTime", OleDbType.Date).Value = CDate(DateTimePicker1.Text)
    End With

    And also the same code for updating, this code since one year ago, Every thing is OK in my app. but yesterday an error occurred when updating the table error says"Convert from Date type to string is not valid" ... I checked my code it is OK the error is not regular, But i tended to change my date type to "DBTimeStamp" like this

    With cmd.Parameters
                    .Add("@DTime", OleDbType.DBTimeStamp).Value = CDate(DateTimePicker1.Text)
    End With

    IS THIS A GOOD IDEA TO AVOID ANY FUTURE ERROR ?

    Any suggestions ...... Thanks in advance

     

    Regards From Amr_Aly

    Friday, March 22, 2019 7:27 AM

Answers

  • Furthermore, why are you using the text property? Hell I had to check twice to see it even had a text property.

    Sorry Mr Gtripopdi,

    I can't understand what do you mean ???????????????????????????



    Regards From Amr_Aly

    CDate(DateTimePicker1.Text)

    Use DateTimePicker1.Value which is of type DateTime instead of converting the Text String into type DateTime

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by Amr_Aly Sunday, March 24, 2019 6:39 AM
    Friday, March 22, 2019 4:03 PM

All replies

  • IS THIS A GOOD IDEA TO AVOID ANY FUTURE ERROR ?

    No it is not optimal.

    Any suggestions ...... Thanks in advance

    Why are you even using a picker to set a timestamp for a database table record?

    Why are you not using the system date and time by using DateTime.Now() in the code based on the system,  a date/time used by the O/S, that can be used by a table column definition defined as Date/Time, which would be the optimal way of setting a timestamp on a table record?

    https://www.dotnetperls.com/datetime-now-vbnet

    • Edited by DA924x Friday, March 22, 2019 10:36 AM
    Friday, March 22, 2019 10:35 AM
  • Thanks DA924x ,

    You are right but i have many datetimepickers used to populate them from a database by this custom format... 

    My original code works well but i noticed that sometimes makes the error and all the time not .... in more details

    I displayed it like this

     And when click on the date it will populate three datetimepickers so I need this scenario ...Hope it reaches my opinion 

    Thanks ....... 


    Regards From Amr_Aly

    Friday, March 22, 2019 12:41 PM
  • Furthermore, why are you using the text property? Hell I had to check twice to see it even had a text property. It's actually not in the property list, seems the only way to access it is in code.

    Try this for your custom format

            dtp_StartTime.CustomFormat = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern & " " &
                Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern
            dtp_EndTime.CustomFormat = Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern & " " &
                Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern
            dtp_StartTime.Format = DateTimePickerFormat.Custom
            dtp_EndTime.Format = DateTimePickerFormat.Custom
            dtp_StartTime.ShowUpDown = True
            dtp_EndTime.ShowUpDown = True
    
            dtp_StartTime.Value = New Date(Now.Year, Now.Month, Now.Day, 0, 0, 0)
            dtp_EndTime.Value = New Date(Now.Year, Now.Month, Now.Day, 23, 59, 59)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 22, 2019 1:08 PM
  • Furthermore, why are you using the text property? Hell I had to check twice to see it even had a text property.

    Sorry Mr Gtripopdi,

    I can't understand what do you mean ???????????????????????????



    Regards From Amr_Aly

    Friday, March 22, 2019 3:51 PM
  • Furthermore, why are you using the text property? Hell I had to check twice to see it even had a text property.

    Sorry Mr Gtripopdi,

    I can't understand what do you mean ???????????????????????????



    Regards From Amr_Aly

    CDate(DateTimePicker1.Text)

    Use DateTimePicker1.Value which is of type DateTime instead of converting the Text String into type DateTime

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by Amr_Aly Sunday, March 24, 2019 6:39 AM
    Friday, March 22, 2019 4:03 PM
  • <<<<Use DateTimePicker1.Value which is of type DateTime instead of converting the Text String into type DateTime>>>

    Did you mean that i should use it like this.

    With cmd.Parameters
                    .Add("@DTime", OleDbType.DBTimeStamp).Value = (DateTimePicker1.Value)
    End With

    Or I should use this one. 

    With cmd.Parameters
                    .Add("@DTime", OleDbType.Date).Value = (DateTimePicker1.Value)
    End With

    Note that my date time picker in a custom formate 

    "dd/MM/yyy hh:mm:ss:tt" ...

    I'll gave it a try


    Regards From Amr_Aly


    • Edited by Amr_Aly Friday, March 22, 2019 5:52 PM
    Friday, March 22, 2019 5:50 PM
  • <<<<Use DateTimePicker1.Value which is of type DateTime instead of converting the Text String into type DateTime>>>

    Did you mean that i should use it like this.

    With cmd.Parameters
                    .Add("@DTime", OleDbType.DBTimeStamp).Value = (DateTimePicker1.Value)
    End With

    Or I should use this one. 

    With cmd.Parameters
                    .Add("@DTime", OleDbType.Date).Value = (DateTimePicker1.Value)
    End With

    Note that my date time picker in a custom formate 

    "dd/MM/yyy hh:mm:ss:tt" ...

    I'll gave it a try


    Regards From Amr_Aly


    Personally I would use

    Command.Parameters.AddWithValue("@Param",DateTimePicker1.Value)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 22, 2019 6:06 PM
  • Excuse me .....

    I have red an article one year ago(((but i don't remember why)))) didn't recommend <<AddWithValue()>> and recommend <<Add("", SqlDBtype.data_type).Value>> i think because this formula mention the data type ...In my opinion it's right datatype in the second formula is important ...In past i used <<AddWithValue()>> but now i'm using <<Add("", SqlDBtype.data_type).Value>> .....

    Data type very important to me in order to ensure that every thing is right

    Thanks  


    Regards From Amr_Aly

    Friday, March 22, 2019 6:37 PM
  • Excuse me .....

    I have red an article one year ago(((but i don't remember why)))) didn't recommend <<AddWithValue()>> and recommend <<Add("", SqlDBtype.data_type).Value>> i think because this formula mention the data type ...In my opinion it's right datatype in the second formula is important ...In past i used <<AddWithValue()>> but now i'm using <<Add("", SqlDBtype.data_type).Value>> .....

    Data type very important to me in order to ensure that every thing is right

    Thanks  


    Regards From Amr_Aly

    AddWithValue will infer the appropriate datatype. You can easily verify that by adding a breakpoint and looking through the Command methods

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 22, 2019 6:44 PM
  • Almr, 

    Try to change your attitude. If you are talking to humans, then use the thousands of way the use date and times, in the way you show.

    If you use computers then use the simply way (ticks from the first year the calendar started (in .Net is used the Gregorian calendar as base). 

    From a DateTimePicker that is the property "Value" which is a 64bit value representing the ticks in 1/100nanoseconds started at the 1:1:1  point of that calendar). 


    Success
    Cor

    Friday, March 22, 2019 8:46 PM
  • Hey Cor, Haven't seen you in a while. Hope you're doing well.

    ... talk some sense into the lad, eh?

    have a good weekend


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 22, 2019 8:53 PM
  • That is correct, don't use 'AddWithValue' use 'Add' and specify the actual datatype. The reason is because it get's inferred and sometimes this is wrong.

    Regards,

    Trevor White

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Friday, March 22, 2019 8:56 PM
  • That is correct, don't use 'AddWithValue' use 'Add' and specify the actual datatype. The reason is because it get's inferred and sometimes this is wrong.

    Regards,

    Trevor White

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    This is bad advice.

    There are rare instances where you will want to explicitly supply a datatype, but this is not one of them. The chance of AddWithValue "getting it wrong" on this date value is 0%

    At the end of the day it comes down to preference. In general I will use AddWithValue for all simple types.

    Also you should not just drop "just dont do it" without some kind of very specific reason other than "sometimes this is wrong" because that makes no sense. If you could elaborate that would be great.


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Friday, March 22, 2019 9:58 PM
    Friday, March 22, 2019 9:20 PM
  • Almr, 

    Try to change your attitude. If you are talking to humans, then use the thousands of way the use date and times, in the way you show.

    If you use computers then use the simply way (ticks from the first year the calendar started (in .Net is used the Gregorian calendar as base). 

    From a DateTimePicker that is the property "Value" which is a 64bit value representing the ticks in 1/100nanoseconds started at the 1:1:1  point of that calendar). 


    Success
    Cor

    Hi, Cor

    Did you mean me sir by changing my attitude .... i didn't understand?

    what was happen to say that or you mean someone else ......


    Regards From Amr_Aly

    Saturday, March 23, 2019 6:42 AM
  • Almr, 

    Try to change your attitude. If you are talking to humans, then use the thousands of way the use date and times, in the way you show.

    If you use computers then use the simply way (ticks from the first year the calendar started (in .Net is used the Gregorian calendar as base). 

    From a DateTimePicker that is the property "Value" which is a 64bit value representing the ticks in 1/100nanoseconds started at the 1:1:1  point of that calendar). 


    Success
    Cor

    Hi, Cor

    Did you mean me sir by changing my attitude .... i didn't understand?

    what was happen to say that or you mean someone else ......


    Regards From Amr_Aly

    Your attitude towards  computers. You do it as if they are humans. 

    Success
    Cor

    Saturday, March 23, 2019 8:45 AM

  • Your attitude towards  computers. You do it as if they are humans. 

    Success
    Cor

    OK , That's it ..........Sorry for misunderstanding....

    I'm trying to understand it well by mistakes and questions ...Hope this method makes me at your level of experience .....Pleased to receive a good advice from you sir ....I think ""Datetimepiker.Value"" is good and perform the mission to save and update date custom formate as <<< Gtripodi>>> mention above 

    Thanks .................... 

     

    Regards From Egypt

    Saturday, March 23, 2019 10:42 AM

  • Use DateTimePicker1.Value which is of type DateTime instead of converting the Text String into type DateTime

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thanks Gtripodi ...................................

    Regards From Egypt

    Sunday, March 24, 2019 6:41 AM
  • Gtripodi,

    Thanks for the feedback! Can you explain bad advice, I was concurring with Amr Aly about using 'AddWithValue' as it is bad practice in general. To be honest, I didn't care to expand on this issue as a simple Google search yields many results, not relevant to the question and the OP clearly has seen it before; why do we need to smear this data elsewhere? Anyways, here's a little list of reasons why not to use AddWithValue.

    1. Performance and concurrency issues.
    2. Infers parameter definition - .Net types do not always map to SQL server types
    3. Infers SqlDbType.NVarChar when a string object is provided because the strings in .Net are Unicode.
    4. Infers SqlDbType.Datetime type when a DateTime object value is provided. A .Net DateTime aligns closely with SQL Server datetime2(7), but AddWithValue uses datetime for backwards compatibility.

    The list goes on and on and to many to list here...

    Sources: Dan Guzman - AddWithValue is Evil | Joel Coehoorn - Can we stop using AddWithValue already?



    Regards,

    Trevor White

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.






    Monday, March 25, 2019 4:07 PM