我使用ajax日曆擴展器和TextChanged事件關注TextBox,我打電話給「txtFromDate_TextChanged」函數。無法將字符串轉換爲日期時間
ASP代碼:
<asp:TextBox ID="txtFromDate" runat="server" AutoPostBack="True"
ontextchanged="txtFromDate_TextChanged"></asp:TextBox>
asp:CalendarExtender ID="txtFromDate_CalendarExtender" Format="dd/MM/yyyy" runat="server" Enabled="True"
TargetControlID="txtFromDate">
</asp:CalendarExtender>
C#代碼:
protected void txtFromDate_TextChanged(object sender, EventArgs e)
{
if (txtFromDate.Text != "")
{
DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);
Query = Query + "And CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, ord_del_date))) >= '" + fromdate.ToString("yyyy'-'MM'-'dd") + "'";
}
}
此代碼工作都很好,當我通過Visual Studio調試的時候我連主辦的網站上我的本地IIS服務器。 的問題是,當我是主持人對我的在線託管服務器現場,當我拿起從壓延機的日期和調用TextChanged事件它提供了以下錯誤:
錯誤:
String was not recognized as a valid DateTime.
我知道它是因爲行給錯誤:
DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);
但我沒有發現此代碼中的任何錯誤。 這段代碼從visual studio或託管在IIS上工作得很好,但我完全困惑爲什麼託管在託管服務器上時它不工作。我完全困惑。請幫助我。
那麼什麼是'txtFromDate.Text'的價值?爲什麼動態構建SQL而不是使用參數化SQL?我的猜測是問題在於服務器使用系統本地文化,並且您應該使用指定不變文化的「DateTime.ParseExact」。 –