-3

I just want to know what is the meaning of this error? The INSERT statement conflicted with the FOREIGN KEY constraint "FK__thumbnail__instr__160F4887". The conflict occurred in database "MusicStoreDB", table "dbo.instrumentItem", column 'instrumentId'. The statement has been terminated.

Here is the stack trace:

Server Error in '/' Application.

The INSERT statement conflicted with the FOREIGN KEY constraint 
"FK__thumbnail__instr__160F4887". The conflict occurred in database 
"MusicStoreDB", table "dbo.instrumentItem", column 'instrumentId'.
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: The INSERT statement 
conflicted with the FOREIGN KEY constraint "FK__thumbnail__instr__160F4887". 
The conflict occurred in database "MusicStoreDB", table 
"dbo.instrumentItem", column 'instrumentId'.
The statement has been terminated.

Source Error: 


Line 74: 
Line 75:             obj.thumbnailImages.Add(subImg);
Line 76:             obj.SaveChanges();
Line 77:         }
Line 78:     }

Source File: c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs    Line: 76 

Stack Trace: 


[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN 
KEY constraint "FK__thumbnail__instr__160F4887". The conflict occurred in 
database "MusicStoreDB", table "dbo.instrumentItem", column 'instrumentId'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean 
breakConnection, Action`1 wrapCloseInAction) +2434922
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, 
Boolean breakConnection, Action`1 wrapCloseInAction) +5736592

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning
(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean 
asyncClose) +285
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, 
SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet 
bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58
System.Data.SqlClient.SqlDataReader.get_MetaData() +89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, 
RunBehavior runBehavior, String resetOptionsString) +379
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, 
Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader 
ds, Boolean describeParameterEncryptionRequest) +2026
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, 
TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean 
asyncWrite) +375
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
+53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, 
String method) +240
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 
behavior) +41
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.
<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c) +9

System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1
.Dispatch(TTarget target, Func`3 operation, TInterceptionContext 
interceptionContext, Action`3 executing, Action`3 executed) +72

System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher
.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) 
+355
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader
(CommandBehavior behavior) +167
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12

System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.
Execute(Dictionary`2 identifierValues, List`1 generatedValues) +234
System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() 
+139

[UpdateException: An error occurred while updating the entries. 
See the inner exception for details.]
System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() 
+319
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.
<Update>b__2(UpdateTranslator ut) +9
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T 
noChangesResult, Func`2 updateFunction) +120
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +77
System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() 
+11
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 
func, IDbExecutionStrategy executionStrategy, Boolean 
startLocalTransaction, Boolean releaseConnectionOnSuccess) +288
System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions 
options, IDbExecutionStrategy executionStrategy, Boolean 
startLocalTransaction) +163
System.Data.Entity.Core.Objects.<>c__DisplayClass2a.
<SaveChangesInternal>b__27() +22
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 
operation) +164

System.Data.Entity.Core.Objects.ObjectContext.
SaveChangesInternal(SaveOptions options, Boolean 
executeInExistingTransaction) +222
System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions 
options) +7
System.Data.Entity.Internal.InternalContext.SaveChanges() +114

[DbUpdateException: An error occurred while updating the entries. See the 
inner exception for details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +199
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +27
System.Data.Entity.DbContext.SaveChanges() +20
ConnectionClassGuitarItems.AddThumnailImage(thumbnailImage subImg) in 
c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs:76
Pages_CreateBrands.itemSaveButton_Click(Object sender, EventArgs e) in 
c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\Pages\CreateBrands.aspx.cs:231
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
+204

System.Web.UI.WebControls.Button.System.Web.UI.
IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, 
String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
Boolean includeStagesAfterAsyncPoint) +1639
Boom Click
  • 51
  • 2
  • 10
  • 1
    The exception message is self explanatory. Make sure the instrumentId you are storingin instrumentItem table is a valid one (it should exist in the Instrument table) – Shyju Sep 16 '17 at 14:55
  • 1
    Are you familiar with what a foreign key constraint is? That would be a good term to look up on Google. Basically the error is telling you that you're trying to insert a value into a column which only allows certain values (based on the values in a related table), and what you're inserting isn't one of the allowed values (doesn't exist in the related table). – David Sep 16 '17 at 14:57

1 Answers1

1

It means that you are trying to insert a row in instrumentItem table and the value that you are assigning to the instrumentId column, is invalid. There is a foreign key relationship between this table and another table in the database and the value of instrumentId should exist in the other table, but it doesn't.

Sparrow
  • 2,548
  • 1
  • 24
  • 28