[英]date saving in SQL using vb.net
I m using the following code to save the date from a textbox and selecting the date using date picker. 我使用以下代码从文本框中保存日期并使用日期选择器选择日期。
If (String.IsNullOrEmpty(DobTxt.Text)) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DBNull.Value
Else
Dim DOBDte As date= String.Format("{0:YYYY-MM-dd}", DobTxt.Text.Trim())
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DOBDte
End If
Now the code works just fine with dates like "" 现在代码工作得很好,日期就像“”
but when you go for a date like "10/01/2016" I get this error: 但是当你去参加像“10/01/2016”这样的约会时,我会收到这个错误:
Conversion from string "10/30/2016" to type 'Date' is not valid 从字符串“10/30/2016”到“日期”类型的转换无效
could you please help 能否请你帮忙
Use TryParse
to convert the text value to a date. 使用
TryParse
将文本值转换为日期。
Dim dateValue As Date
If String.IsNullOrWhiteSpace(DobTxt.Text) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DBNull.Value
ElseIf Date.TryParse(DobTxt.Text.Trim(), dateValue) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = dateValue
Else
' alert the user that there is invalid input
End If
If you're using jQuery date picker, and your date format is not mm-dd-yy you should configure datepicker to that format: 如果您使用的是jQuery日期选择器,并且日期格式不是mm-dd-yy,则应将datepicker配置为该格式:
$( ".selector" ).datepicker({
dateFormat: "yy-mm-dd"
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.