当前位置:编程学习 > C#/ASP.NET >>

从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界

刚站添加数据时经常出现以下问题,但过一会儿又没事,究竟是什么问题请高手指教一下

从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
语句已终止。 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: 从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
语句已终止。

Source Error: 


Line 219: {
Line 220: count = -1;
Line 221:                throw;
Line 222: }
Line 223: finally
 

Source File: e:\web\fc\jc2\App_Code\DataAccessLayer\Database.cs    Line: 221 

Stack Trace: 


[SqlException (0x80131904): 从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
语句已终止。]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857434
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +735046
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +380
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   Fjzf.DataAccessLayer.Database.ExecuteSQL(String SqlString) in e:\web\fc\jc2\App_Code\DataAccessLayer\Database.cs:221
   Fjzf.DataAccessLayer.Database.Insert(String TableName, Hashtable Cols) in e:\web\fc\jc2\App_Code\DataAccessLayer\Database.cs:336
   Fjzf.BusinessLogicLayer.Commodity.Add(Hashtable ht) in e:\web\fc\jc2\App_Code\BusinessLogicLayer\Commodity.cs:192
   ClientMS_issue.btoOK_Click(Object sender, EventArgs e) in e:\web\fc\jc2\ClientMS\issue.aspx.cs:96
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 
--------------------编程问答-------------------- tryparse一下,还有你跟下值不就知道了。程序问题。 --------------------编程问答-------------------- 很明显,你想输入的datetime,但是定义的是char,他不允许转换 --------------------编程问答-------------------- 这个问题不是经常出现只是一时时出现的 --------------------编程问答-------------------- 那你在程序中在传这个值得时候先做个转换,然后再转换这里加try cache
然后你就会知道了 --------------------编程问答-------------------- 贴代码出来 一般  比如说你给你字符串   "2010-2-29" 转的时候就会出错! 因为2月没29日 就会出错 。
  如果是“2010-02-28”就不会错 。 你把代码贴出来 --------------------编程问答-------------------- 可以基本确定是由于你给你字符串 时间或者是 日期部分超出了界限 仔细检查下    你可以加个
try 
 cdatetime(string)
catch 
throw  string    --------------------编程问答--------------------
引用 6 楼 b3727180 的回复:
可以基本确定是由于你给你字符串 时间或者是 日期部分超出了界限 仔细检查下 你可以加个
try 
 cdatetime(string)
catch 
throw string



如果是日期错会直接发不到 但现在问题是有时候能发,有时候出错 --------------------编程问答-------------------- 你可以试试加个成功失败的日志
看看成功和失败的字符串有什么区别 --------------------编程问答-------------------- 出错的语句应该是这个

public int ExecuteSQL(String SqlString)
{
int count = -1;
Open();
try
{
SqlCommand cmd = new SqlCommand(SqlString,Connection);
count = cmd.ExecuteNonQuery();
}
catch
{
count = -1;
                throw;
}
finally
{
Close();
}
return count;
} --------------------编程问答-------------------- SqlString的内容是什么? --------------------编程问答-------------------- SqlString 是SQL UPDATE字符串 --------------------编程问答--------------------
引用 10 楼 liuyileneal 的回复:
SqlString的内容是什么?

要的是你char字符串和update的有关的那个拼接。。 --------------------编程问答--------------------     protected void bto_Click(object sender, EventArgs e)
    {
        string imgurl = "";
        if (ddlKind2.SelectedValue=="0")
        {
            Common.Alert(Page,"请选择类目");
            return;
        }
        if (FileUpload1.FileName != "")
        {
            imgurl = com.UploadImg(FileUpload1, 100, "Commodity/images/", HiddenField1.Value,Page);
            if (imgurl == "")
                return;
        }
        Hashtable ht = new Hashtable();
        Common.CheckClient(Session, Response);
        ht.Add("C_User", SqlHelper.GetQuotedString(Session["JCMS_LoginName"].ToString()));
        ht.Add("C_Name", SqlHelper.GetQuotedString(txtName.Text.Trim()));
        if (Session["JCMS_BrandID"] != null && Session["JCMS_BrandID"].ToString() != "")
            ht.Add("C_BrandID", Session["JCMS_BrandID"].ToString());
        ht.Add("C_KindID",ddlKind2.SelectedValue);
        ht.Add("C_Price",txtPrice.Text);
        ht.Add("C_Size",SqlHelper.GetQuotedString(txtSize.Text.Trim()));
        ht.Add("C_ImgUrl", SqlHelper.GetQuotedString(imgurl));
        ht.Add("C_Details", SqlHelper.GetQuotedString(txtDetails.Text));
        ht.Add("C_ServicesID", "'" + FW() + "'");
        ht.Add("C_CreateDate", SqlHelper.GetQuotedString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ff")));
        ht.Add("C_Way", rblWay.SelectedValue);
        ht.Add("Offers_Var", "'"+ Offers_Var.Text +"'");
        if(rblWay.SelectedValue=="1")
            ht.Add("C_AddDate", SqlHelper.GetQuotedString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ff")));
        if (Commodity.Add(ht))
        {
            ClearText();
            Database database_cx = new Database();
            DataSet dsm = DataConnectServer("select top 1 ID from [Commodity] where C_User='" + Session["JCMS_LoginName"].ToString() + "' order by ID desc");
            if (dsm.Tables[0].Rows.Count != 0)
            {
                NeiRong NeiRong = new NeiRong();
                NeiRong.testid(dsm.Tables[0].Rows[0]["ID"].ToString(), "../JCHTML/");
                DowFiles(int.Parse(dsm.Tables[0].Rows[0]["ID"].ToString()));
            }
            Common.Alert(Page, "发布成功");
        }
        else
            Common.Alert(Page, "发布失败");
    } --------------------编程问答--------------------
引用 11 楼 renbanana 的回复:
SqlString 是SQL UPDATE字符串

我说的是内容啊。。。 --------------------编程问答-------------------- SQL Server 中的日期值范围在1753-1-1至9999-12-31,检查日期范围. --------------------编程问答-------------------- char 类型?

你试试

string time = "2010-05-20";

DateTime = convert.to.....(time)
--------------------编程问答-------------------- 最好是在那个代码的地方放入
try catch finally  来扑捉你的异常,
这个时候你可以调试一下,看看你给出的东西,可以转换成时间吗?

或者是:
DateTime.Parse();
            DateTime .TryParse()
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,