none
An exception occur while updating products RRS feed

  • Question

  • Im working with .Net Mvc 4, while editing my entries i got this error.

    My Error:

    The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_catalog_product_catalog_categories". The conflict occurred in database "DemoE-commerce", table "dbo.catalog_categories", column 'id'.
    The statement has been terminated.

    My Edit Controller Code:

    //EDIT PRODUCT
     
            [HttpPost]
            public ActionResult edit_product(catalog_product cat_product)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        db.Entry(cat_product).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("cat_product");
                    }
                }
                catch (Exception e)
                { throw e; }
                return View();
            }

    i need to know how to update data with foreign key table relation, am i missing some piece of code while updating..

    need help!!!

    Wednesday, February 14, 2018 1:35 PM

All replies

  • Hi Faseeh12,

    >>i need to know how to update data with foreign key table relation, am i missing some piece of code while updating..

    According to your code, it seems good, but based on your error message, it seems that you pass wrong argument, which cause a foreign key constraint issue. I would suggest that you could check if pass the right argument.

    You could also monitor the SQL statement executed in SQL server via SQL Server profile if you use SQL Server.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 15, 2018 2:30 AM
    Moderator
  • Maybe, you are doing something in the object that breaks the relationship between parent and child, like child no longer points back to the parent by parent's primary ID.
    Thursday, February 15, 2018 6:36 AM