Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlCommand cmd = new SqlCommand("insert into tblUserRole(UserRoleName,CreatedDate,ModifiedDate) values(@RoleName,@CreateDate,@ModifyDate)", con);
            //SqlCommand cmd = new SqlCommand("insert into tblUserRole(UserRoleName) values(@RoleName)", con);

            string strcreateDate = System.DateTime.Now.ToString();
            DateTime c;
            c = new DateTime();
            c = DateTime.ParseExact(strcreateDate, "yyyy-MM-dd HH:mm tt ", null);

here it is giving error
String was not recognized as a valid DateTime.


i am inserting in sql server 2012 .can someone help..i just need to take date from system and store in string pass that string as parameter to wcf service and store date in sql server2012 through webservice
Posted
Updated 23-Oct-12 3:41am
v3
Comments
psychic6000 23-Oct-12 9:33am    
"6/15/2008" throws an exception where "06/15/2008" will not, you might be having this problem

Hi,

this should do:
C#
c = DateTime.Parse(value);


To convert date to string with format, I would use
C#
var formattedDateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
 
Share this answer
 
Comments
Vygandas Pliasas 23-Oct-12 9:44am    
To use universal date formatting, use CultureInfo.InvariantCulture
If you use ParseExact, then it expects the string format date to match exactly the format you give it - and clearly strcreateDate does not do that. You could try DateTime.Parse (or DateTime.TryParse for the non-erroring version, it reports success via a bool return value instead), but the fact that you are getting an error implies that your system is not set to use "yyyy-MM-dd HH:mm tt" as it's current culture.

If you do use Parse or TryParse, they will also expect the current culture, so your code snippet above will work.

Or, you could provide the format to ToString and produce a string in the expected format directly:
C#
string strcreateDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm tt");


But the way I would do it is to pass the DateTime value through to SQL directly, without converting it to a string at any point - SQL understands DateTime objects and provided you hand it over via a parametrized query to an Sql DateTime field, it will work without any conversion.
 
Share this answer
 
You could try using DateTime.Parse instead of DateTime.ParseExact.
 
Share this answer
 
C#
private static DateTime ParseDate(string s)
       {
           DateTime result;
           if (!DateTime.TryParse(s, out result))
           {
               result = DateTime.ParseExact(s, "yyyy-MM-ddT24:mm:ssK", System.Globalization.CultureInfo.InvariantCulture);

           }
           return result;
       }
 
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