如何转换用户输入的日期和时间并保存在SQL Server中

时间:2014-09-07 05:21:17

标签: c# sql-server datetime

通过使用带有AjaxToolkit MaskedTextextender的StartDate,我有EndDate<asp:TextBox>的日期时间输入文本框。 EnddateStartDate代码相同。

这是代码。

<asp:TextBox ID="txtStartDateTime" runat="server"></asp:TextBox>

<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1"
    TargetControlID="txtStartDateTime" runat="server" Mask="99/99/9999 99:99"
    MessageValidatorTip="true" MaskType="DateTime"  ClearMaskOnLostFocus="true"
    ClearTextOnInvalid="true" AcceptAMPM="true" UserDateFormat="DayMonthYear"
    ErrorTooltipEnabled="True">
</ajaxToolkit:MaskedEditExtender>

<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator1" runat="server" 
    ControlToValidate="txtStartDateTime" ControlExtender="MaskedEditExtender1"
    IsValidEmpty="false" EmptyValueMessage="Input Date and Time" 
    InvalidValueMessage="Date Time Input Not Valid">
</ajaxToolkit:MaskedEditValidator> 

然后,我有c#代码,我从这些输入获取值并保存在数据库的表中。这是我的c#代码。

string strStartDateTime = txtStartDateTime.Text;   
DateTime dtStartingDateTime = new DateTime();    
dtStartingDateTime = DateTime.ParseExact(strStartDateTime, "dd/MM/yyyy hh:mm",
    System.Globalization.CultureInfo.InvariantCulture);

现在,问题是将这些日期保存到数据库时出错。这是错误消息。

  

System.FormatException:String未被识别为有效的DateTime。

     

at System.DateTimeParse.ParseExact(String s,String format,   DateTimeFormatInfo dtfi,DateTimeStyles样式)

     

at System.DateTime.ParseExact(String s,String format,   IFormatProvider提供商)

我尝试了convert.ToDateTime(....)。它不起作用。我完全迷失了如何在SQL Server中保存日期和时间。任何人都可以帮我解决这个问题吗?

0 个答案:

没有答案