The INSERT statement conflicted with the FOREIGN KEY constraint

Posted by Shailesh21235 under ASP.NET on 6/22/2012 | Points: 10 | Views : 8694 | Status : [Member] | Replies : 4
i have a table Table_Product which has primary key "id" and foreign key "categoryid" with table Table_Category 's "Id"
when execute command "insert into Table_Product values('"+txtCategoryid.Text+"','"+txtProductname.Text+"','"+txtPrice.Text+"','"+txtImagepath.Text+"','"+txtDescription.Text+"','"+datetime+"','"+datetime+"')";
it shows error

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Table_Product_Table_Category". The conflict occurred in database "dblist", table "dbo.Table_Category", column 'Id'.

what's the error???




Responses

Posted by: Sheonarayan on: 6/23/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
If you have constraint specified in the table, you should ensure that you are inserting the same id into first table that exists into the second table (constraint table - foreign key table) otherwise you will get error.

Looks like the data you are going to insert doesn't exist into the foreign key table.

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dkummar on: 6/26/2012 [Member] Starter | Points: 25

Up
0
Down
hi Shailesh21235,

the foreign key constraint specified for the table Table_Product with Table_Category so u have to insert the value for +txtCategoryid.Text will be
in constraint table..




N.dinesh kummar
Software Developer

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 6/26/2012 [Member] Starter | Points: 25

Up
0
Down
This error Says that the value you are trying to insert (Category ID) doesnot exists in the table u have refered.Kindly check to that table whether the id u r trying to insert is already existing or not.

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muralidosscm on: 6/26/2012 [Member] Starter | Points: 25

Up
0
Down
first Insert the values in the parent table and then insert the values in child table.............

error is Category ID does not exist in the parent table



Regards
Muralidoss M

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response