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
- 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.SelectedValue;
- 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();