publicstaticDateTime DateParse(string date)
{
date = date.Trim();
if (!string.IsNullOrEmpty(date))
returnDateTime.Parse(date, new System.Globalization.CultureInfo("en-GB"));
returnnewDateTime();
}
Mark as Answer, if the post helped you.
Cheers,
Mayank Arora
Marked as answer by hiteshgolu015 on Aug 10, 2011 10:56 AM
I observed an unusual behavior with this solution. It works fine when input date is 03/07/2011, but it gives "String was not recognized as a valid DateTime."
error when input date is 13/07/2011. What could be the reason for it and how do I solve it ?
hiteshgolu01...
Member
23 Points
9 Posts
Convert c# string to SQL Datetime.
Aug 10, 2011 09:42 AM|LINK
Hi,
I have a textbox on a page where user fills date in DD/MM/YYYY format.
I need to insert this date in a SQL table column, where date format is sql Datetime (e.g. 10/1/2007 12:00:00 AM)
How do i convert c# string to this format ?
I am getting this error while executing insert command : "String was not recognized as a valid DateTime."
Please help on this.
navneetmitaw...
Contributor
2967 Points
933 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 09:48 AM|LINK
hi frnd you can use
Navneet Kumar Mitawa
MayankArora
Member
290 Points
56 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 09:51 AM|LINK
Hi use this codeCheers,
Mayank Arora
hiteshgolu01...
Member
23 Points
9 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 09:52 AM|LINK
Thnx Dude. It worked.
So simple it was.
hiteshgolu01...
Member
23 Points
9 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 10:40 AM|LINK
@Mayank :
Everything is correct except that it is giving me format : mm/dd/yyyy instead of dd/mm/yyyy.
Can it be corrected ?
hiteshgolu01...
Member
23 Points
9 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 10:55 AM|LINK
Ok.
I solved it by myself.
Instead of using en-GB, i tried en-US and it gave the correct format.
hiteshgolu01...
Member
23 Points
9 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 04:53 PM|LINK
I observed an unusual behavior with this solution. It works fine when input date is 03/07/2011, but it gives "String was not recognized as a valid DateTime." error when input date is 13/07/2011. What could be the reason for it and how do I solve it ?
konanki
Contributor
6198 Points
1239 Posts
Re: Convert c# string to SQL Datetime.
Aug 10, 2011 06:21 PM|LINK
in insert statement you can convert database datetime.
CONVERT(DATETIME,"datestring",103).