日期:2014-05-17  浏览次数:20541 次

该字符串未被识别为有效的 DateTime。
C# code



行 24:             cusInfo.CusPwd = txtPwd.Text;
行 25:             cusInfo.RightId = 1;
行 26:             cusInfo.Birthday =DateTime .Parse ( txtBirthday.Text);//此行出错
行 27:             cusInfo.Address = txtAddress.Text;
行 28:             cusInfo.Gender = ddlGender.SelectedValue;

源文件: F:\毕业设计\webShop\WebShop\Register.aspx.cs    行: 26 

堆栈跟踪: 


[FormatException: 该字符串未被识别为有效的 DateTime。]
   System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2844238
   System.DateTime.Parse(String s) +25
   WebShop.Register.btnRegister_Click(Object sender, EventArgs e) in F:\毕业设计\webShop\WebShop\Register.aspx.cs:26
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565





把出错行换成
cusInfo.Birthday = Convert.ToDateTime(txtBirthday.Text);
cusInfo.Birthday = Convert.ToDateTime(txtBirthday.Text.trim());
cusInfo.Birthday =DateTime.Parse(txtBirthday.Text.trim())也出现同样的错误,数据库中birthday为datetime类型

------解决方案--------------------
txtBirthday.Text的值不是一个格式正确的日期字符串,检查一下,确保其格式正确。
------解决方案--------------------
断点看看txtBirthday.Text是什么,能否为空,要么就做个异常处理
------解决方案--------------------
txtBirthday.Text.tostring("yyyy-mm-dd hh:mm:ss")
------解决方案--------------------
探讨

引用:

txtBirthday.Text的值不是一个格式正确的日期字符串,检查一下,确保其格式正确。

那样写就正确了。为什么要正确的格式呢?怎么才能随便写一个字符串类的让他自动转换,如“19900520”这个