I get this error: "String or binary data would be truncated" when I try to
insert data in the database. But the stack trace gives no clue as to what is
the offending data column. How can see the offending column?
Stack Trace
System.Data.SqlClient.SqlException was unhandled by user code
Message="String or binary data would be truncated.\r\nThe statement has
been terminated."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=8152
Procedure=""
Server="jones\\sqlexpress"
State=2
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader
ds, RunBehavior runBehavior, String resetOptionsString)
at
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method,
DbAsyncResult result)
at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand
command, DataSourceOperation operation)
at
System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values)
at System.Web.UI.WebControls.SqlDataSourceView.Insert(IDictionary
values)
at System.Web.UI.WebControls.SqlDataSource.Insert()
at CreateUser.CreateUserWizard1_CreatedUser(Object sender, EventArgs
e) in c:\Documents and Settings\Llewelyn Jones\My Documents\Visual Studio
2005\webRetail\CreateUser.aspx.cs:line 33
at System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs
e)
at System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser()
at
System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e)
at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source,
EventArgs e)
at System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object
source, EventArgs e)
at
System.Web.UI.WebControls.Wizard.WizardChildTable.OnBubbleEvent(Object
source, EventArgs args)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args)
at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Signature
L. A. Jones
Eliyahu Goldin - 15 Jul 2007 08:52 GMT
The error is reported by the sql server whereas the stack trace comes from
your app. That's why you don't see in the stack any additional info. Catch
the actual query sent to the server either with the database tracer or with
a breakpoint in the application and see what column has a too long value.

Signature
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
>I get this error: "String or binary data would be truncated" when I try to
> insert data in the database. But the stack trace gives no clue as to what
[quoted text clipped - 77 lines]
> at System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Alexey Smirnov - 15 Jul 2007 08:56 GMT
> I get this error: "String or binary data would be truncated" when I try to
> insert data in the database. But the stack trace gives no clue as to what is
[quoted text clipped - 73 lines]
> --
> L. A. Jones
The Insert statement of the SqlDataSource was failed, because some of
your columns could be bigger than the defined size, and db doesn't
allow you to insert data larger than a column definition. You should
either 1) perform validation to ensure that the form input parameters
are correct or 2) conform the data to the required internal db
structure.
1) Use MaxLength property, or a RegularExpressionValidator
<asp:TextBox MaxLength="X"
2) Use Left function
INSERT .... Left(@parameter,X).....