Click here to Skip to main content
11,924,280 members (66,249 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET ADO.NET LINQ C#4.0 , +
I am getting this error only where a value contains '&'. I don't know why this is happening as '&' operator is ok to insert into SQL server.

Here is the detailed Error log -
System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated.
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
Posted 13-Oct-12 0:48am
Nitesh Luharuka 13-Oct-12 6:08am
Are you sure it happens because of "&" symbol?
Are you using plain query or Stored Procedure to submit data to Db?
ssure1singh 16-Oct-12 1:40am
Yes I am sure, it happens because of "&" symbol and I am using Linq to Sql in .Net 4.0. It's strange but true.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

"string or binary data would be truncated" - This Error Means the length of field of table is smaller then the length of data that is to be inserted in Table.

Eg- if length of field is VARCHAR(10);
& the data that is to be inserted is - "Welcome to the world of ASP.NET" i.e it's length is more than 10.
So, we need to increase the length of the field.
This has noting to do with '&'.
ridoy 13-Oct-12 6:40am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Error like this usually means that some string value is to long.
For example when you define column:


and you would try to insert string with length>100 you get error like this.

Check your data against this.
ssure1singh 13-Oct-12 6:09am
Yes I understand that but I have already checked for value sizes as per the database tables and all seems good aspect that one value contains '&' which is causing this error.
n.podbielski 13-Oct-12 6:21am
Maybe this sign is unicode and its size is really 2 bytes and your column is char(xx).
Then even if length of string is correct this will be to much for DB. Check this in sql script because I am just guessing now.
ssure1singh 13-Oct-12 6:31am
Yes I checked same values inserting into database by sql script instead of LINQ to SQL and values inserted corrected without issue.

So I am suspecting error with LINQ to SQL. Is something with OR mapping details which store in DBML file?
n.podbielski 13-Oct-12 6:50am
Then maybe LinqToSQL do something funny with data. I would check with SQL Profiler what statement it's generating exactly. When you gonna see that maybe cause and solution will be obvious.
ssure1singh 16-Oct-12 1:42am
I just added temp solution for now and will look into this once get some time to do R&D with LinqToSql. Anyway Thanks for suggestion.
Member 11428695 14-Feb-15 1:10am
I've specified more length for the column than the value i'm passing in textbox.. still i'm getting the same error???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.3 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100