Оператор INSERT конфликтует с ограничением внешнего ключа "FK_subdepartment_subdepartment". Конфликт произошел в базе данных "attendancemanagement", таблице "dbo.department", столбце
у меня возникла проблема при вставке данных в sql с помощью c# через dropdownlist fK.
Что я уже пробовал:
protected void Page_Load(object sender, EventArgs e) { { con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement"); using (SqlCommand cmd = new SqlCommand("SELECT DeptID, DeptName FROM Department")) { cmd.CommandType = CommandType.Text; cmd.Connection = con; con.Open(); DropDownList1.DataSource = cmd.ExecuteReader(); DropDownList1.DataTextField = "DeptName"; DropDownList1.DataValueField = "DeptID"; DropDownList1.DataBind(); con.Close(); } } DropDownList1.Items.Insert(0, new ListItem("--Select Customer--", "0")); } protected void Button1_Click(object sender, EventArgs e) { string DeptName = txtsubdept.Text; string DeptID = DropDownList1.SelectedItem.Text; string query = "INSERT INTO SubDepartment VALUES(@DeptName,@DeptID)"; con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement"); { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@DeptName", DeptName); cmd.Parameters.AddWithValue("@DeptID", DeptID); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close();