字符串未被识别为从索引0开始的有效日期时间/未知字词? [英] String Was Not Recognized As A Valid DateTime / Unknown Word Starting At Index 0?
问题描述
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(日期);
// ddlYear.SelectedValue +/+ ddlMonth.SelectedValue +/+ ddlDate.SelectedValue
SqlCommand cmd = new SqlCommand(@插入OnlineBookingEvent(BookingEvent,BookeventDate,cdt,udt)值
(@ 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() ;
如果您的每个下拉列表都有一个数值,那么为什么要通过这种环形交叉方法将其构建为DateTime?为什么转换依赖于当前系统设置(您不参考或检查)通过构建字符串?
而是尝试:
DateTime dt = new DateTime(( int ) ddlYear.SelectedItem.Value,( int )ddlMonth.SelectedItem.Value,( int )ddlDate.SelectedItem。价值);
这样,没有字符串,没有解释 - 只是清晰度(以及更可读的代码才能启动)?
Convert.ToDateTime考虑您的系统日期时间格式。
例如,假设您的机器日期时间格式为dd / MM / yyy 并且你的字符串包含以yyyy / MM / dd表示的日期时间,你将得到上述错误。
更改系统日期时间格式。
或者使用以下代码然后执行exec来更改应用程序DateFormat上面的代码
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;
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();
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:
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)?
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;
这篇关于字符串未被识别为从索引0开始的有效日期时间/未知字词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!