Reputation: 132
I have an ASP.NET MVC2 application on .NET 4.0 and built using VS 2010 that has two tables (amongst others), an employees
table which has the following columns:
Firstname, MiddleName, LastName, NationalID, EmployeeType, MobileNumber, Email and UserId.
The userId
field is a foreign key from the aspnet_users
table which happens to be a guid. I am trying to persist the employee details for a selected user but cannot do so as I am getting an error.
The guid value for the selected user can be extracted and displayed using the ViewData dictionary. The userId
guid value is 23aa8cd9-7552-403c-8c99-fcdd611cb188
.
I would like to persist this value together with other employee properties but when I submit the form I get the error shown below. I am also using EF4 for my model. Here's the stacktrace:
System.Data.UpdateException was unhandled by user code
Message=An error occurred while updating the entries. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at TechDispatch.Models.EmployeeRepository.SaveChanges() in C:\Projects\TechDispatch\TechDispatch\Models\EmployeeRepository.cs:line 131
at TechDispatch.Models.EmployeeRepository.AddNewEmployee(Employee Add) in C:\Projects\TechDispatch\TechDispatch\Models\EmployeeRepository.cs:line 127
at TechDispatch.Controllers.EmployeeController.Create(Employee employee) in C:\Projects\TechDispatch\TechDispatch\Controllers\EmployeeController.cs:line 69
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Data.SqlClient.SqlException
Message=String or binary data would be truncated.
The statement has been terminated.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=8152
Procedure=""
Server=.
State=13
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()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
InnerException:
Could anyone please assist?
Upvotes: 0
Views: 1040
Reputation: 4608
To me, this message means that you are adding a string that is too long for one of your char/varchar fields:
String or binary data would be truncated. The statement has been terminated.
Upvotes: 2