Search code examples
asp.netc#-4.0datetimeajaxcontroltoolkittextchanged

Unable to cast String to DateTime


I've following TextBox with ajax calender extender and on TextChanged event I am calling "txtFromDate_TextChanged" function.

asp code:

<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# code:

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") + "'";

  }
}

This code is working all fine when i debug through visual studio and even when I host the site on my local IIS server. The problem is that when I host the site on my online hosting server and when I pick a date from calender and calls a textchanged event it gives following error:

Error:

String was not recognized as a valid DateTime.

I know it is giving error because of line:

DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);

But I am not finding anything wrong in this code. This code is working perfectly fine from visual studio or when hosted on IIS but I am totally confused why it isn't working when hosted on hosting server. I am totally confused. Please help me out.


Solution

  • DateTime fromdate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
    
    Query = Query + "And   CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, ord_del_date))) >= '" + fromdate.ToString("yyyy'-'MM'-'dd") + "'";