Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to send date from my web page in dd/mm/yyyy format for that i write this:
C#
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
   {
       TextBox2.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
   }

My question is When i select a date as 23/1/1984 from my asp.net calendar control(don't want to use ajax control) and in my BL(business logic) i create a property as
C#
public DateTime DOB
       {
           get { return _DOB; }
           set { _DOB = value; }
       }

i created a method Insert() and write:
parameters[1] = new SqlParameter("@DOB", SqlDbType.DateTime) { Value = DOB };

in code behind file i make a object of BL as (
BL obj = new BL();
)
i on button click assign value to DOB as that i hv enterd in textbox as
Quote:
obj.DOB = DateTime.Parse(TextBox2.Text);

but on execution when say i enter 23/12/1988 and click submit button following error appears ::
Quote:
String was not recognized as a valid DateTime.


What is wrong in this why it is occuring?
Posted

datetime.parse want mm/dd/yyyy fromat and u r sending dd/mm/yyyy

u can change
TextBox2.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");


or
use culture to convert datetime format
 
Share this answer
 
v2
Comments
Rambo_Raja 26-Jun-13 6:22am    
it is easy i know but my boss has told me to show date of birth in dd/mm/yyyy.
DateTime.Parse supports many formats. It is versatile.

check this linke .
C# DateTime.Parse
 
Share this answer
 
See my previous post. hope it will be usefull.

Conversion of string to date time in c# asp.net[^]
 
Share this answer
 
If you want to parse datetime string that is independent form current culture (or any culture), you can use ParseExact[^].
 
Share this answer
 
Refer detailed answer for similar question on CP: String was not recognized as a valid DateTime.[^]
 
Share this answer
 

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