ASP.Net AJAX CalendarExtender Error: String was not recognized as a valid DateTime

Last Reply 7 months ago By dharmendr

Posted 7 months ago

i am using ajax calender extender.

<asp:CalendarExtender ID="cal_ext_1" TargetControlID="txt_attendance"  Format="dd/mmm/yyyy" runat ="server"></asp:CalendarExtender>
cmd.Parameters.AddWithValue("@EMP_ATTENDANCE", Convert.ToDateTime(txt_attendance.Text)); 

getting error

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: String was not recognized as a valid DateTime.

Posted 7 months ago

Hi Nandhu,

Check this example. Now please take its reference and correct your code.

HTML

<asp:ScriptManager runat="server" />
<asp:TextBox ID="txt_attendance" runat="server" />
<asp:CalendarExtender ID="cal_ext_1" TargetControlID="txt_attendance" Format="dd/MMM/yyyy"
    runat="server">
</asp:CalendarExtender>
<asp:Button Text="Save" runat="server" OnClick="Save" />

Code

C#

protected void Save(object sender, EventArgs e)
{
    DateTime dt = DateTime.ParseExact(txt_attendance.Text, "dd/MMM/yyyy", null);
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + dt + "')", true);
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As DateTime = DateTime.ParseExact(txt_attendance.Text, "dd/MMM/yyyy", Nothing)
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & dt & "')", True)
End Sub

Screenshot