String or binary data would be truncated. The statement has been terminated.[SqlException (0x80131904): String or binary data would be truncated

0

I have already searched for answer saying the inserted value could be long or mismatch of datatype, due to that this type of error could come but this web app works fine on my local system but when I use it after publishing it to the web, this error shows up but all other web page insert queries work just fine

Error shows up on only this one page.

String or binary data would be truncated.
The statement has been terminated.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +388
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4515
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
System.Data.SqlClient.SqlDataReader.get_MetaData() +134
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6557689
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +6560327
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource
1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +586
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +104
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +288
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +171
System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues) +303
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +485

[UpdateException: An error occurred while updating the entries. See the inner exception for details.]

System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +4502203
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +583
System.Data.Entity.Internal.InternalContext.SaveChanges() +218

[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +291
frm_addkarigar.btnadd_Click(Object sender, EventArgs e) +1672
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

And this is the c# code to insert the data

db_mytailorEntities _objdb = new db_mytailorEntities();
int count = _objdb.tbl_karigar.Where(obj => obj.name == txtname.Text).Count();

karigar.name = txtname.Text;
karigar.state = ddlstate.Text;
karigar.city = ddlcity.Text;
karigar.address = txtaddress.Text;
karigar.ph1 = txtph1.Text;
karigar.ph2 = txtph2.Text;
karigar.date_reg = DateTime.Now.ToString();
karigar.name = txtname.Text;

if (txtamntdue.Text == "")
    karigar.amount_due = 0;
else
    karigar.amount_due = int.Parse(txtamntdue.Text);

karigar.K_id = txtname.Text+"-"+(count+1);
karigar.foruser = Session\["userid"\].ToString().TrimEnd();

_objdb.tbl_karigar.Add(karigar);
_objdb.SaveChanges();

Response.Write("<script language>alert('Karigar added successful')</script>");][1]
c#
sql-server-2012
asked on Stack Overflow Mar 10, 2016 by Juned Khan • edited Mar 10, 2016 by marc_s

3 Answers

1

This error suggests that the data you're trying to write is too big for one of the columns. It works locally but not in your production environment, which could be a few things. Is the schema the same on your local box as your production environment? Do you know what data is being written that causes the error?

answered on Stack Overflow Mar 10, 2016 by Jason Taylor
0

I had a similar error, String or binary data would be truncated, but it was from a SQL table CRUD permissions being removed by a DBA. So I'd look at the connection string, and the web site (IIS) Authentication / Impersonation settings, if nothing else work. It works from you local machine, but not from the web server. So a quick check is to enter your credential under IIS impersonation, or inside the web.config connection string.

answered on Stack Overflow Mar 10, 2016 by Henry L
0

I had the same problem. In my case, the problem will be in the place where I did not expect an error. I forgot to make the id "identity", in order to it be incremental. Yes, so simply.

answered on Stack Overflow Aug 17, 2020 by Grek

User contributions licensed under CC BY-SA 3.0