Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this Code and When I convert textbox.text to DateTime it throw an exception
(String was not recognized as a valid DateTime).

ASP.NET
<asp:TextBox ID="TextBoxBirthDate" runat="server" CssClass="tb10"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                             ControlToValidate="TextBoxBirthDate" 
                             ErrorMessage="Please enter your Date of Birth" Display="Dynamic">*</asp:RequiredFieldValidator>
                          <asp:CalendarExtender ID="CalendarExtender1" runat="server" DefaultView="Years" 
                             FirstDayOfWeek="Saturday" Format="dd/MM/yyyy" 
                             TargetControlID="TextBoxBirthDate" EndDate="1997-12-31" 
                             StartDate="1980-01-01" SelectedDate="1994-01-01">
                         </asp:CalendarExtender>



please help
Posted

Your format is asking for a day / month / year format. You are giving it a selected date in the format year / day / month. Change your format to yyyy/dd/mm.
 
Share this answer
 
v2
Comments
kareem salem 17-Apr-12 6:16am    
thanks Dominic, but it's Arabian culture that day comes before month
The most obvious issue here is that you have this code:
HTML
asp:CalendarExtender ID="CalendarExtender1" runat="server" DefaultView="Years" 
  FirstDayOfWeek="Saturday" Format="dd/MM/yyyy" 
  TargetControlID="TextBoxBirthDate" EndDate="1997-12-31" 
  StartDate="1980-01-01" SelectedDate="1994-01-01"
Well, your EndDate, StartDate and SelectedDate aren't in the format dd/MM/yyyy. Change them to this:
HTML
EndDate="31/12/1997" StartDate="01/01/1980" SelectedDate="01/01/1994"
 
Share this answer
 
Comments
kareem salem 17-Apr-12 6:35am    
thanks pete O'Hanlon I think I need fill
Convert.ToDateTime method with IformatProvider but I don't know how
Convert.TodDateTime(TextBoxBirthDate,[xxxxxxx]);
Try using:
C#
DateTime.ParseExact(TextBoxBirthDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
 
Share this answer
 
Comments
kareem salem 18-Apr-12 2:42am    
thank you sjelen
DateTime.ParseExact(TextBoxBirthDate.Text, "yyyy/MM/dd", CultureInfo.InvariantCulture);

why yyyy/MM/dd format always shows error
 
Share this answer
 
Hi,

TextBox text may be in incorrect format for date. Try giving it in "mm/dd/yyyy" format.
 
Share this answer
 
Comments
[no name] 17-Apr-12 16:08pm    
In this context, mm/dd/yyyy would mean minutes/days/year. MM is month.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900