The INSERT statement conflicted with the FOREIGN KEY C #

1

I have this code to insert values into a table:

conn.Open();
comm.CommandText = @"INSERT INTO ArticleBarCode(Code, Code_Article, BarCode, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, IsDeleted)
                   VALUES (@code, @codearticle, @barcode, 1, @date, 1, @date, 0)";
comm.Parameters.AddWithValue("@code", next);
comm.Parameters.AddWithValue("@codearticle", code);
comm.Parameters.AddWithValue("@barcode", numbercode);
comm.Parameters.AddWithValue("@date", DateTime.Now);
comm.ExecuteNonQuery();
conn.Close();

But when I run it gives me this error:

  

The INSERT statement conflicts with the FOREIGN KEY constraint   "FK_ArticleBarCode_Article". The conflict occurred in database   "CardozuGestDB", table "dbo.Article", column 'Code'.

How can I resolve this?

    
asked by anonymous 28.04.2017 / 12:00

1 answer

3

Well, the error says that you are violating the constraint of the foreign key by inserting a value that does not exist in the reference table. In other words, the value that is passing to the Code column does not exist in the Article table.

You have to see your model to say what the field is, because your insert just does not say exactly. If you are unsure of the field please review the% with% FK_ArticleBarCode_Article.

    
28.04.2017 / 12:51