Can anyone explain this mistake to me?

Asked

Viewed 184 times

-2

I have 2 tables, products and suppliers. And is appearing the following message:

System.Data.Sqlclient.Sqlexception: 'The INSERT statement conflicted with the FOREIGN KEY Constraint "Fk_products_supplier". The Conflict occurred in database "C: USERS TIAGO DOCUMENTS VISUAL STUDIO 2017 PROJECTS PROJECTS WORK FINAL POO WORK FINAL POO BD.MDF", table "dbo.Supplier", column 'cnpj'.

Can someone explain me this mistake?

  • 2

    The message is stating what the error: You are trying to insert a product whose data is key to the headphone (cnpj) is not valid - IE, there is no registered supplier with the informed cnpj

  • 1

    If still, there is some doubt, include your code to the question

2 answers

2

The error is quite clear, foreign key conflict... You are trying to insert in the table [Produtos] a product with a Supplier that does not exist, in the case in the column CNPJ, which seems to be its fk

1

Do the Insert in the [Suppliers] table before doing the Insert in the [Products] table. With this, the database will be able to locate the record of the vendor you are trying to reference in that Sert that is showing error.

  • I can’t, it’s error in the syntax and the ta command like this: string inserts = "INSERT INTO " + table + " VALUES (" + y.txt1cadforn.Text + ",'" + y.txt2cadforn.Text.Tostring() + "','" + y.txt3cadforn.Text.Tostring() + "','" + y.txt4cadforn.Text.Tostring() + "');"; You have an error?

  • Probably the syntax error is due to the quotes. Try the following: "INSERT INTO " + table + " VALUES ('" + y.txt1cadforn.Text + "','" + y.txt2cadforn.Text.ToString() + "','" + y.txt3cadforn.Text.ToString() + "','" + y.txt4cadforn.Text.ToString() + "');"; However, by checking this chunk of your code I imagine that you are concatenating the result of the form fields directly to run in the database, this can cause you serious security problems if it is not a test application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.