Tuesday, March 1, 2011

How to insert foreign key value into table

I want to insert the product in the product table but the product table is also having a category Id which is the foreign key ,How will I insert the foreign key through code please tell me.

i have used this syntax

NewItemToInsert.tbl_PRODUCT_CATEGORY.category_id = Convert.ToInt32 (categoryId);

Categories are displayed in the dropdown list on the add product page and to bind that dropdown I have written a class.

Category Id which I want to insert already exists in the Category table and that Id I want to add into Product table

Please give me useful suggesstions

Thanks Ritz

From stackoverflow
  • you need to have the category table in memory (or use a proper enum that is in sync with the values in the db), and set the right categoryid when you do the update / insert command. if you post the code, we can see if there is any problem there.

  • CategoryId you want to insert in Product table, should already exist in ProductCategory table.

    marc_s : not just "should" - it **must** exist in the ProductCategory table! Otherwise there's no FK relationship.....

0 comments:

Post a Comment