Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
conn.Open();
string date = Convert.ToString(ddlYear.SelectedItem.Value) + "/" + Convert.ToString(ddlMonth.SelectedItem.Value) + "/" + Convert.ToString(ddlDate.SelectedItem.Value);
DateTime dt = Convert.ToDateTime(date);
// ddlYear.SelectedValue + "/" + ddlMonth.SelectedValue + "/" + ddlDate.SelectedValue
SqlCommand cmd = new SqlCommand(@"insert into OnlineBookingEvent(BookingEvent,BookeventDate,cdt,udt)values
(@BookingEvent,@BookeventDate,@cdt,@udt)", conn);
cmd.Parameters.AddWithValue("@BookingEvent", ddlEventName.DataValueField);
cmd.Parameters.AddWithValue("@BookeventDate",dt);
cmd.Parameters.AddWithValue("@cdt", System.DateTime.Now);
cmd.Parameters.AddWithValue("@udt", System.DateTime.Now);
cmd.ExecuteNonQuery();
Posted
Updated 18-Jan-20 1:05am

If you have a Value to each of your drop down lists which is numeric, why are you building it into a DateTime by such a roundabout method? Why do conversions which are dependant on the current system settings (which you don;t reference or check) by building a string at all?

Instead try:
C#
DateTime dt = new DateTime((int) ddlYear.SelectedItem.Value, (int) ddlMonth.SelectedItem.Value, (int) ddlDate.SelectedItem.Value);


That way, there is no strings, not interpretation - just clarity (and more readable code to boot)?
 
Share this answer
 
Comments
Matt T Heffron 11-Nov-13 15:33pm    
+5
Convert.ToDateTime considers your system datetime format.

For instance lets say your machine date time format is "dd/MM/yyy" and your string contain date time in "yyyy/MM/dd" you will get above error.


Either change system date time format.
Or change Application DateFormat using following code and then execute above code

CultureInfo current = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
DateTimeFormatInfo ObjPriDateTimeFormat = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo;
ObjPriDateTimeFormat.ShortDatePattern="yyyy/MM/dd";
current.DateTimeFormat = ObjPriDateTimeFormat;
Thread.CurrentThread.CurrentCulture = current;
 
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