Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is bizarre, I have created a SQL Server Express Project in Visual Studio 2017 and am now using SQL Server Management Studio to enter some data into the Database Tables.

I have entered the first three Rows in my Leagues Table but the 4th (Jackson) is rejecting, despite the data being identical ... please help !!!

I am trying to add data to the Leagues Database & the Foreign Key link is to the LeagueTypes Id & is the first data after the name, it's 1 in all 4 entries.

LeagueTypes Data is as follows ...

Quote:
1 SNAIL
2 EMAIL
3 ONLINE
4 PREMIER
5 VETERANS
NULL NULL


Leagues Data is as follows ...

Quote:
3 CHURCHILL 1 19 0 16 4 12 2018-01-06
4 DISRAELI 1 19 0 16 4 12 2018-01-16
5 GLADSTONE 1 19 0 16 4 12 2018-01-09
NULL JACKSON 1 19 0 16 4 12 2018-01-15
NULL NULL NULL NULL NULL NULL NULL NULL NULL


The Error Message is "The data in Row 4 was not committed. Error source: .Net SQL Client Data Provider. Error Message: The INSERT statement conflicted with the FOREIGN KEY constraint FK_Leagues_LeagueTypes. The conflict occurred in database KADB, Table dbo.LeagueTypes, Column Id. The statement has been terminated."

What I have tried:

Googled the error, checked the structure, checked the data
Posted
Updated 7-Jan-18 2:38am

1 solution

Look at the error message:
The INSERT statement conflicted with the FOREIGN KEY constraint FK_Leagues_LeagueTypes. The conflict occurred in database KADB, Table dbo.LeagueTypes, Column Id.
The table has a Foreign key relationship with a different table, and you are trying to insert a value to the ID column which does not (or more likely cannot) exist in that table.
Check your table definitions, and make sure you haven't got the foreign keys the wrong way round or similar: If you can't work it out, post the actual SQL table definitions and show the actual data that currently exists, plus the INSERT statement you are using that fails.
 
Share this answer
 
Comments
Gary Heath 7-Jan-18 8:43am    
But why did the first 3 Rows go in just fine ?
Gary Heath 7-Jan-18 8:50am    
OK, it looks like I have done something wrong here, the Column I am entering 1 into in Leagues, to pick up Id=1 from the LeagueTypes Table isn't referenced in the Foreign Key statement ... I've never used VS in this way before, I've obviously got some more learning to do !!!
Gary Heath 7-Jan-18 8:51am    
Thanks Griff
OriginalGriff 7-Jan-18 9:55am    
You're welcome!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900