Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using ajaxtoolkit:calender extender for selecting date ,the date format for calender extender is mm/dd/yyyy and my machine date format is mm/dd/yyyy.When i select the any date less than 13 then data is saved in database but when i select the date greater than 12 then a messgae is showing in my application which says that my date format is wrong.

ASPX Code:

XML
<asp:TextBox ID="txtdob" runat="server" CssClass="textb" MaxLength="100" Width="150px"></asp:TextBox>&nbsp
                     <asp:Label ID="Label25" runat="server" CssClass="tranlbl" Text="mm/dd/yyyy"></asp:Label>
                     <ajaxToolkit:CalendarExtender ID="dob" runat="server" TargetControlID="txtdob" Format="MM/dd/yyyy" Enabled="True"></ajaxToolkit:CalendarExtender>
                     <asp:RequiredFieldValidator ID="rfddob" runat="server"  Text="DOB Can Not Be Blank" ControlToValidate="txtdob" CssClass=" validatortextcenter"></asp:RequiredFieldValidator>



CS Code

insert into table (dob) values ('" + datetime.parse(txtdob.text).TosSring("yyyy-MM-dd") + "')

When the date is 03/08/2014 the date is inserting into database but when i select the date greater than 12 then i got error.Please help me !!!
Posted
Comments
joshrduncan2012 10-Mar-14 15:16pm    
insert into table (dob) values ('" + datetime.parse(txtdob.text).TosSring("yyyy-MM-dd") + "')

What is "TosSring"? That should be a red flag right there.
Sandip Paul 491984 10-Mar-14 15:24pm    
sry it was typing mistake
it is datetime.parse(txtdob.tex).tostring("yyyy-mm-dd")

use this

IFormatProvider Provider = new System.Globalization.CultureInfo("en-US", true);
convert.toDateTime(txtdate.text,provider)
 
Share this answer
 
Try this:

DateTime doj = new DateTime(); 
System.Globalization.CultureInfo c1 = new System.Globalization.CultureInfo("en-GB", true);
            doj = DateTime.Parse(txtdob.Text.Trim(), c2, System.Globalization.DateTimeStyles.NoCurrentDateDefault);


and insert like this
C#
insert into table (dob) values ('" + doj.ToShortDateString()+ "')
 
Share this answer
 
v3
I would suggest to use


XML
<asp:TextBox ID="txtStartDate" runat="server" Width="31%"></asp:TextBox>
                 <Ajax:calendarextender id="CalStartDate" runat="server" format="yyyy-MM-dd" targetcontrolid="txtStartDate"></Ajax:calendarextender>
                 <asp:Label ID="lbl_Validate_SD" runat="server" ForeColor="Red"></asp:Label>
 
Share this answer
 
Thats the reason experts always recommend to use ISO 8601 date format for server side processing. Check this

ISO_8601[^]

ISO 8601 Format[^]
 
Share this answer
 
I would suggest to use stored procedure[^], instead the queries in code behind.
There are 2 resons:
1) possible SQL Injection[^]
2) date time format depends on localization[^] (culture).

Ad 1)
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]


Ad 2)
Globalizing and Localizing .NET Framework Applications[^]
How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization[^]
Formatting Date and Time for a Specific Culture[^]
Formatting Numeric Data for a Specific Culture[^]


Finally, your stored procedure might looks like this:
SQL
CREATE PROCEDURE InsertMyData
    @myDate DATETIME
AS
BEGIN
    --temporary change 
    SET DATEFORMAT ymd; --or dmy or mdy
    INSERT INTO TableName (Field) VALUES(@myDate)

END
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 15:39pm    
Good points, a 5.
—SA
Maciej Los 10-Mar-14 16:10pm    
Thank you, Sergey ;)

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