Akhter HUssain

Akhter HUssain

  • 719
  • 1.3k
  • 102.3k

FK conflict occurred

Dec 19 2018 4:30 AM
This error is coming
 
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_SubDepartment_SubDepartment". The conflict occurred in database "AttendanceManagement", table "dbo.Department", column
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. {  
  4. con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement");  
  5. using (SqlCommand cmd = new SqlCommand("SELECT DeptID, DeptName FROM Department"))  
  6. {  
  7. cmd.CommandType = CommandType.Text;  
  8. cmd.Connection = con;  
  9. con.Open();  
  10. DropDownList1.DataSource = cmd.ExecuteReader();  
  11. DropDownList1.DataTextField = "DeptName";  
  12. DropDownList1.DataValueField = "DeptID";  
  13. DropDownList1.DataBind();  
  14. con.Close();  
  15. }  
  16. }  
  17. DropDownList1.Items.Insert(0, new ListItem("--Select Customer--""0"));  
  18. }  
  19. protected void Button1_Click(object sender, EventArgs e)  
  20. {  
  21. string DeptName = txtsubdept.Text;  
  22. string DeptID = DropDownList1.SelectedValue;  
  23. string query = "INSERT INTO SubDepartment VALUES(@DeptName,@DeptID)";  
  24. con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement");  
  25. {  
  26. using (SqlCommand cmd = new SqlCommand(query))  
  27. {  
  28. cmd.Parameters.AddWithValue("@DeptName", DeptName);  
  29. cmd.Parameters.AddWithValue("@DeptID", DeptID);  
  30. cmd.Connection = con;  
  31. con.Open();  
  32. cmd.ExecuteNonQuery();  
  33. con.Close();  

Answers (1)