1

I need help about this issue. There is a problem but I could not find it

error message is

Cannot add or update a child row: a foreign key constraint fails (db_kiosk.tbl_oyunhareketi, CONSTRAINT OH_KioskID FOREIGN KEY (OH_KioskID) REFERENCES tbl_kiosk (Kiosk_ID) ON DELETE NO ACTION ON UPDATE NO ACTION)

Here is my code c#

string sql = "insert into db_kiosk.tbl_oyunhareketi (OH_OyuncuID,OH_KioskID,OH_Puan,OH_Tarih,OH_ControlRow) values ('"+Convert.ToInt32(Label4.Text)+"','"+Convert.ToInt32(Label3.Text)+"','" + Puan_txt.Text + "' , '" + Tarih_txt.Text + "',1)";

Label3.Text = Kiosk_Drop.SelectedValue;

Label4.Text = Oyuncu_Drop.SelectedValue;

Kiosk_Drop and Oyuncu_Drop are the dropdownlist in ASP.net and they are in the selectedIndexChanged function. When I display the labels the values are coming properly but problem is insertion. DB attributes also integer but I couldn't find the problem.

usmanali
  • 2,028
  • 2
  • 27
  • 38
OykunYenal
  • 85
  • 4
  • 14

1 Answers1

1

You have foreign key constraint that prevents you from adding rows to the child table before you have relevant information in the parent table.

In this case you should first populate tbl_kiosk before adding a row to tbl_oyunhareketi. Rows are matched by columns OH_KioskID and Kiosk_ID.

vhu
  • 12,244
  • 11
  • 38
  • 48
  • tbl_kiosk already populated in the system, I created a query inside the Workbench of Mysql. It is taken the values from the tbl_kiosk but in the code section it is not working. – OykunYenal Aug 25 '15 at 08:17
  • Can you show content of the `sql` variable after, so that it shows how `Convert.ToInt32(Label3.Text)` and other variables are expanded. – vhu Aug 25 '15 at 08:20
  • Error message means that outcome of the `Convert.ToInt32(Label3.Text)` doesn't exist as value in `Kiosk_ID` column of the `tbl_kiosk`. – vhu Aug 25 '15 at 08:22