Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

is there a size limit to this cause im getting an error that i cant figure out

I create an sql string as so, add parameters to it and execute it:

string cmdstr = "INSERT INTO locations(id1, id2, companyname, address, city, province, postalcode, phonenumber, faxnumber, contact, contactemail) VALUES (@id1,@id2,@companyname,@address,@city,@province,@postalcode,@phonenumber,@faxnumber,@cont act,@contactemail)";

SqlCommand sqlCmd = GetCommandSQL(cmdstr);

sqlCmd.CommandTimeout = TimeOut;

sqlCmd.Parameters.Add("@id1", SqlDbType.Int).Value = itID;

sqlCmd.Parameters.Add("@id2", SqlDbType.Int).Value = Convert.ToInt32(ddlDPCLocation.SelectedValue);

sqlCmd.Parameters.Add("@companyname", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbCompanyName")).Text;

sqlCmd.Parameters.Add("@address", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbAddress")).Text;

sqlCmd.Parameters.Add("@city", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbCity")).Text;

sqlCmd.Parameters.Add("@province", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbProvince")).Text;

sqlCmd.Parameters.Add("@postalcode", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbPostalCode")).Text;

sqlCmd.Parameters.Add("@phonenumber", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbPhoneNumber")).Text;

sqlCmd.Parameters.Add("@faxnumber", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbFaxNumber")).Text;

sqlCmd.Parameters.Add("@contact", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbContact")).Text;

sqlCmd.Parameters.Add("@contactemail", SqlDbType.VarChar).Value = ((TextBox)dvShippingInformation.FindControl("tbContactEmail")).Text;

sqlCmd.ExecuteNonQuery();

for testing purposes ive added the max text in each textbox area, so each textbox has 50 characters or so

and i get an error message as follows, when executing the query:

"System.Data.SqlClient.SqlException: String or binary data would be truncated.\r\nThe statement has been terminated.\r\n at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)\r\n at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)\r\n at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)\r\n at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)\r\n at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)\r\n at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()\r\n at _default.InsertGKShippingLocation() in c:\\Inetpub\\wwwroot\\cleanapp\\default.aspx.cs:line 125\r\n at _default.InsertOrder() in c:\\Inetpub\\wwwroot\\cleanapp\\default.aspx.cs:line 93\r\n at _default.InsertandSend() in c:\\Inetpub\\wwwroot\\cleanapp\\default.aspx.cs:line 178"

Anybody have any ideas why this is happening
[3689 byte] By [clean] at [2007-11-11 8:13:37]
# 1 Re: is there a size limit to this cause im getting an error that i cant figure out
Hi.

This is an MSSQL issue - when running an INSERT INTO statement, you need to make sure the sum of all lengths of all insered values doesn't exceed ~8000 bytes (I don't recall the exact number, maybe I'm mistaken and it's ~16,000 bytes, but you get the idea).

I'd guess you have many large varchar() or char() fields in your table, and these are summing up.
You could try passing in trimmed values, or use text() columns instead (each text column only takes up several bytes since it holds a pointer to the text and not the actual text).

- Avi
avinap at 2007-11-11 23:13:20 >
# 2 Re: is there a size limit to this cause im getting an error that i cant figure out
i was sleeping on that one, forgot i made some database structure changes and the size of some fields got smaller forgot to change the front end maxlength on the fields to reflect the database changes
clean at 2007-11-11 23:14:20 >