简体   繁体   English

字符串或二进制数据将被截断。

[英]String or binary data would be truncated.?

I'm building an input form in a C# web app. 我正在使用C#Web应用程序构建输入表单。 I can submit it just fine but I get the System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated error. 我可以提交它,但我得到System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated错误。 I know its a length issue but Ive double checked everything but just cant seem to figure this out here. 我知道这是一个长度问题,但我已经仔细检查了一切,但似乎无法在这里解决这个问题。

{
    // Open Connection
    thisConnection.Open();

    // Create INSERT statement with named parameters
    nonqueryCommand.CommandText = "INSERT  INTO InventoryInput (Date, Item, Qty, WStatus) VALUES (@Date, @Qty, @Item, @WStatus)";


    // Add Parameters to Command Parameters collection
    nonqueryCommand.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
    nonqueryCommand.Parameters.Add("@Item", System.Data.SqlDbType.VarChar, 255);
    nonqueryCommand.Parameters.Add("@QTY", System.Data.SqlDbType.NChar, 10);
    nonqueryCommand.Parameters.Add("@WStatus", System.Data.SqlDbType.VarChar, 50);


    nonqueryCommand.Parameters["@Date"].Value = TextBox1.Text;
    nonqueryCommand.Parameters["@Item"].Value = DropDownList1.Text;
    nonqueryCommand.Parameters["@QTY"].Value = TextBox2.Text;
    nonqueryCommand.Parameters["@WStatus"].Value = DropDownList3.Text;
    nonqueryCommand.ExecuteNonQuery();

}

在此输入图像描述

You've mixed up two of your columns: 你混淆了两个列:

(Date,  Item, Qty,   WStatus) VALUES
(@Date, @Qty, @Item, @WStatus)

You're trying to insert your @Qty into the Item column (okay), and @Item into the Qty column (probably wrong). 你试图将你的@Qty插入Item列(好吧),并将@Item插入Qty列(可能是错误的)。

I also agree with comments that some of the data types still look suspect - Eg a non-numeric quantity. 我也同意一些数据类型仍然可疑的评论 - 例如非数字数量。

Make sure your inputs aren't too long with Substring() : 使用Substring()确保输入不会太长:

nonqueryCommand.Parameters["@Date"].Value = TextBox1.Text;
nonqueryCommand.Parameters["@Item"].Value = DropDownList1.Text.Substring(0, 255);
nonqueryCommand.Parameters["@QTY"].Value = TextBox2.Text.Substring(0, 10);
nonqueryCommand.Parameters["@WStatus"].Value = DropDownList3.Text.Substring(0, 50);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串或二进制数据将被截断。 在SqlDataReader上 - String or binary data would be truncated. on SqlDataReader 字符串或二进制数据将被截断。 声明已终止?8 - String or binary data would be truncated. The statement has been terminated?8 字符串或二进制数据将被截断。 语句已终止。[SqlException (0x80131904): 字符串或二进制数据将被截断 - String or binary data would be truncated. The statement has been terminated.[SqlException (0x80131904): String or binary data would be truncated 获取“字符串或二进制数据将被截断。 插入新记录时,该语句已终止” - Getting “String or binary data would be truncated. The statement has been terminated” when inserting new record 执行SqlCommand失败,因为“字符串或二进制数据将被截断。 语句已终止”错误 - Executing SqlCommand fails due to “string or binary data would be truncated. the statement has been terminated” error 如何解决“字符串或二进制数据将被截断。\r\n语句已终止。” 错误? - How to solve the "String or binary data would be truncated.\r\nThe statement has been terminated." error? 字符串或二进制数据将被截断。 但我无法以其他方式保存它 - String or binary data would be truncated. but i can't save it other way 字符串或二进制数据将被截断。 该语句已终止。 在数据库表中插入记录时 - String or binary data would be truncated. The statement has been terminated. while inserting records in DB table 字符串或二进制数据将被截断。\\ r \\ n该语句已终止。 C# - String or binary data would be truncated.\r\nThe statement has been terminated. c# 导致插入错误的选择性Xml索引:字符串或二进制数据将被截断。 该语句已终止 - Selective Xml Index causing Insert Error: string or binary data would be truncated. the statement has been terminated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM