DataType Erorr:字符串或二进制数据将被截断

时间:2016-05-03 19:55:36

标签: c# sql asp.net sql-server asp.net-web-api

Asp.net cs C#代码是

if (FileUpload1.HasFile)
{
    try
    {
        //File Name
        string fn = FileUpload1.PostedFile.FileName;
        //File Save
        FileUpload1.SaveAs(Server.MapPath("~/PagesWith/FilesDiv/") + fn);
        //File Path
        string fPath = HttpContext.Current.Server.MapPath("~/PagesWith/FilesDiv/") + fn;
        //File Type
        string ft = FileUpload1.PostedFile.ContentType;
        // Get the length of the file.
        int fileLen = FileUpload1.PostedFile.ContentLength;
        //File Size
        // Create a byte array to hold the contents of the file.
        byte[] input = new byte[fileLen - 1];
        input = FileUpload1.FileBytes;
        SqlParameter[] Prm = new SqlParameter[7];
        Prm[0] = new SqlParameter("@fname", txtFileNm.Text);
        Prm[1] = new SqlParameter("@fsize", input);
        Prm[2] = new SqlParameter("@ftype", ft);
        Prm[3] = new SqlParameter("@categoryID", ddlCategory.SelectedValue);
        Prm[4] = new SqlParameter("@subject", txtSubject.Text);
        Prm[5] = new SqlParameter("@comments", txtComments.Text);
        Prm[6] = new SqlParameter("@fpath", fPath);
        DB_Functions.ExcuteNonQuery("SP_A_Data", Prm);
        lblState.Text = "Your Data is Saved (: ";
        clear();
        lblState.Text = "Added";
    }
    catch( Exception ex)
    {
        lblState.Text = ex.ToString();
    }
}
else 
{
    lblState.Text="Erorr";
}

数据库>>存储过程是

ALTER PROCEDURE [dbo].[SP_A_Data]
(
    @fname varchar(100)=null,
    @fsize int=null,
    @ftype varchar(100)=null,
    @categoryID int=null,
    @subject varchar(50)=null,
    @comments varchar(500)=null,
    @fpath varchar(100)=null
)
AS
BEGIN
  Insert Into Data_Divs(fname,fsize,categoryID,[subject],comments,ftype,fpath)
  Values(@fname,@fsize,@categoryID,@subject,@comments,@ftype,@fpath)
End

Erorr

字符串或二进制数据将被截断。 enter image description here

1 个答案:

答案 0 :(得分:1)

我想对此进行评论,但更明确的是作为答案。这可以帮助您找到问题所在。

... some code here

    Prm[5] = new SqlParameter("@comments", txtComments.Text);
    Prm[6] = new SqlParameter("@fpath", fPath);

        // add this loop into your code and trace the program. 
        foreach (var item in Prm)
        {
            var len = item.Value.ToString().Length;
            // check len to see if it is bigger than of size of the corresponding column
            // if len is bigger than your column size, that is the issue
            // and you need to alter that column to store bigger string
        }


    DB_Functions.ExcuteNonQuery("SP_A_Data", Prm);
    lblState.Text = "Your Data is Saved (: ";
    clear();
    lblState.Text = "Added";

... some code here

如果帖子已回答问题,请“标记为答案”