Reputation: 2388
I'm building an input form in a C# web app. I can submit it just fine but I get the System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated
error. 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();
}
Upvotes: 2
Views: 8053
Reputation: 239824
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).
I also agree with comments that some of the data types still look suspect - E.g. a non-numeric quantity.
Upvotes: 7
Reputation: 50865
Make sure your inputs aren't too long with 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);
Upvotes: 2