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'.
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'.
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.
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
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?
Member
60 Points
246 Posts
Saving dates to SQL
Jun 27, 2019 08:38 PM|Dan5|LINK
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',
});
All-Star
43701 Points
18703 Posts
Re: Saving dates to SQL
Jun 27, 2019 08:52 PM|mgebhard|LINK
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
Member
60 Points
246 Posts
Re: Saving dates to SQL
Jun 28, 2019 09:31 AM|Dan5|LINK
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
All-Star
43701 Points
18703 Posts
Re: Saving dates to SQL
Jun 28, 2019 10:23 AM|mgebhard|LINK
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?
Member
60 Points
246 Posts
Re: Saving dates to SQL
Jun 28, 2019 11:57 AM|Dan5|LINK
Thank you, working now..