0

I have one table [Software] and have 2 foreign key [Brand_ID,Category_ID]. I have bonded the 2 table [Brand and Category] with a drop down list.

When the insert statement is executing I am getting the INSERT statement conflicted with the FOREIGN KEY constraint error.

Here is the code:

Insert INTO tblSoftware(Description,Date_Of_Purchase,Price,Brand_ID,Category_ID) Values (@Description,@Date_Of_Purchase,@Price,@Brand_ID,@Category_ID)", con);   

When I remove the Brand_ID and Category_ID from the sql statement, it is inserting fine.

How do i solve this?

Thanks

nexusmusic00
  • 35
  • 1
  • 5
  • 2
    Without the table definitions and the values attempting to be inserted this is not really an answerable question. But this has been answered dozens and dozens of times....even in the title you realize this has been answered before. – Sean Lange May 02 '17 at 13:28
  • Are the values of @Brand_ID,@Category_ID existing into parent tables ? – ahmed abdelqader May 02 '17 at 13:28
  • values of @Brand_ID,@Category_ID are existing parent tables then only we can't insert data into table because of having referential integrity –  May 02 '17 at 13:30
  • Try searching next time, there are at least six answered questions regarding this error on SO alone. – RamblinRose May 02 '17 at 13:32
  • 1
    Possible duplicate of [INSERT statement conflicted with the FOREIGN KEY constraint](http://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint) – Kostis May 02 '17 at 13:58

2 Answers2

1

@nexusmusic00, make sure when you execute insert statement at that time Brand_ID and Category_ID values exists in Brand and Category table respectively.

Leena
  • 76
  • 4
0

What you are passing for @Brand_ID,@Category_ID when user did not select any thing from dropdown. I guess you are passing 0 for those two parameters that cause issue you have to set that value as NULL.

Kevin Shah
  • 1,589
  • 1
  • 13
  • 20