locked
Saving dates to SQL RRS feed

  • Question

  • User1028962535 posted

    Hello, I am using a datepicker attached to a text box to select the date and the eventually insert it in a sql table. It works fine if the format look like this 
    dateFormat: 'yyyy/mm/dd', however I would prefer users to enter the date like this dd/mm/yyyy in the textbox, when I change the function to reflect the different format, SQL throws a a conversion error..how do I get the date to look on way on the form 'dd/mm/yyyy', but saved as a date datatype in SQL like 'yyyy/mm/dd'. 

    $(function() {
    $('#' + '<%= txtdob.ClientID %>').datepicker({
    dateFormat: 'yyyy/mm/dd',
    });

    Thursday, June 27, 2019 8:38 PM

Answers

  • User475983607 posted

    Either the txtdob.Text has the current date or there is an issue with your SQL.  We cannot see either so is there anyway you share the SQL run your code through the debugger?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 10:23 AM

All replies

  • User475983607 posted

    Hello, I am using a datepicker attached to a text box to select the date and the eventually insert it in a sql table. It works fine if the format look like this 
    dateFormat: 'yyyy/mm/dd', however I would prefer users to enter the date like this dd/mm/yyyy in the textbox, when I change the function to reflect the different format, SQL throws a a conversion error..how do I get the date to look on way on the form 'dd/mm/yyyy', but saved as a date datatype in SQL like 'yyyy/mm/dd'. 

    $(function() {
    $('#' + '<%= txtdob.ClientID %>').datepicker({
    dateFormat: 'yyyy/mm/dd',
    });

    Convert the date picker input to a DateTime type.  Then save the DateTime not the string that represents a DateTime.  Keep in mind the string representation of a date depends on the current culture settings.

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8

    Thursday, June 27, 2019 8:52 PM
  • User1028962535 posted

    Thanks for replying, I tried the code below,but it always passes todays date instead of the date in the textbox, (the time is correct though) , what am I doing wrong?

    If (String.IsNullOrEmpty(txtdob2.Text)) Then
    comm.Parameters.Add(New SqlParameter("@DOB", DBNull.Value))
    Else
    Dim d As String = txtdob2.Text
    Dim dt = DateTime.ParseExact(d, "dd-MM-yyyy", Nothing)
    comm.Parameters.Add(New SqlParameter("@DOB", dt))
    End If

    Friday, June 28, 2019 9:31 AM
  • User475983607 posted

    Either the txtdob.Text has the current date or there is an issue with your SQL.  We cannot see either so is there anyway you share the SQL run your code through the debugger?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 10:23 AM
  • User1028962535 posted

    Thank you, working now..

    Friday, June 28, 2019 11:57 AM