我在网上查了产生这个错误的原因,说是:
 
    如果该字段类型是varchar(10),在该字段输入的数据长度是15,那么就会产生这个异常。    可是我还真查不到到是哪个字段出现了问题,请求大家帮忙解答~一下是异常的堆栈跟踪、我的数据表、我输入信息、以及后台代码:[SqlException (0x80131904): 将截断字符串或二进制数据。
语句已终止。]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +946986
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +821638
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +149
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +404
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) +447
   System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +72
   System.Web.UI.WebControls.FormView.HandleInsert(String commandArg, Boolean causesValidation) +388
   System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +602
   System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e) +109
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
   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) +5087表名是:goods;s_id为用户ID,t_id为物品类型ID
这是我输入的数据:
下面是后台代码:
 protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
//该事件是在执行插入操作之前先设置物品编号g_id、发布信息的用户s_id、物品图片g_image、物品类型t_id。    {
        FileUpload fileobj;
        SqlDataReader dr;
        string sqlstr;        string thepath = Server.MapPath("g_image");//以下是对图片的上传操作      
        fileobj = (FileUpload)FormView1.FindControl("pic");
        if (fileobj.HasFile)
        { 
           if (!Directory .Exists (thepath ))
           {
               Directory.CreateDirectory(thepath);
           }
           fileobj.SaveAs(thepath + "\\" + fileobj.FileName);           e.Values["g_image"] = fileobj.FileName;        }//以下是获取用户的标识
        e.Values["s_id"] = HttpContext.Current.User.Identity.Name.ToString();//以下是对物品类型的操作
        DropDownList thedrop;
        thedrop = (DropDownList)FormView1.FindControl("type");
        string thesel = thedrop.SelectedValue;
        e.Values["t_id"]=thesel ;//以下是对物品编号的操作
        string newgoods_id = System.DateTime.Now.Year.ToString() + ((System.DateTime.Now.Month < 10) ?("0"+System .DateTime .Now .Month .ToString ()):System .DateTime .Now .Month .ToString ()) +((System .DateTime .Now .Day <10)? ("0" + System.DateTime.Now.Day.ToString()) : System.DateTime.Now.Day.ToString());
 
        sqlstr = "select * from goods where left(g_id,8)='" + newgoods_id + "' order by g_id desc";   
        SqlConnection con = new SqlConnection("data source=localhost;Initial Catalog=campustrade;integrated security=SSPI");
        con.Open();
        SqlCommand com = new SqlCommand(sqlstr , con);
        com.CommandType = CommandType.Text;
        dr =  com.ExecuteReader(CommandBehavior.CloseConnection);
             if (dr.Read())
        {
            string str1;
            int i;
            int j;            str1 = (int.Parse(dr["g_id"].ToString().Substring(8, 4)) + 1).ToString();
            j = str1.Length;
            for (i = 0; i < 4 - j; i++)
            {
                str1 = "0" + str1;
            }
            newgoods_id = newgoods_id + str1;
        }
        else
        {
            newgoods_id = newgoods_id + "0001";
        }        e.Values["g_id"] = newgoods_id;
        dr.Close();
   con.Close();
        
    }    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)//完成插入操作后的触发
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('数据添加成功!');window.location='user_goods_public.aspx';", true);
    }

解决方案 »

  1.   

    你看一下插入的sql语句,调试一下代码看一下插入时各个参数都是什么,对比一下数据库就知道是哪个字段对应的参数溢出了
      

  2.   

    我觉得是你同varchar()的问题,里面的数字其实指的是西文的长度,而不是中文的
    你应该用nvarchar() varchar(4) 可以输入4个字线,也可以输入两个汉字
    nvarchar(4) 可以输四个汉字,也可以输4个字母,但最多四个
      

  3.   

    这是数据库绑定的代码,插入语句:
    <asp:SqlDataSource ID="SqlDataSource_goods_public" runat="server" 
       InsertCommand="insert into goods(s_id,t_id,g_title,price_now,g_intro,new_or_old,price_int,g_image)            values(@s_id,@t_id,@g_title,@price_now,@g_intro,@new_or_old,@price_int,@g_image)"
       ConnectionString="<%$ ConnectionStrings:campustradeConnectionString %>" >
        <InsertParameters >
            <asp:Parameter Name ="g_id" DefaultValue = "" Type ="String" />
            <asp:Parameter Name ="s_id" />
            <asp:Parameter Name ="g_image" DefaultValue ="none.gif" Type ="String" /> 
            <asp:Parameter Name ="t_id" DefaultValue ="" Type ="String" />
                        </InsertParameters> 
                    </asp:SqlDataSource>