i am using linq statement to update data in database. first user will search for name and the details will be displayed in gridview and then user will select one employee and the data will be displayed in the relative textbox and dropdowns . when i select data in both dropdown it works fine and the database gets updated and if i select only one dropdown it shows error "The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Emp_Location". The conflict occurred in database "EmpDept", table "dbo.Location", column 'LocationID'."

//Update Button

using (DataClassesDataContext dc = new DataClassesDataContext())
                Emp employee = dc.Emps.Single(emp => emp.EmpID == id);

                employee.EmpFirstName = txtFirstName.Text;
                employee.EmpLastName = txtLastName.Text;
                employee.MgrID = int.Parse(txtMgrID.Text);
                employee.BirthDate = Convert.ToDateTime(txtDOB.Text);
                employee.Email = txtEmail.Text;
                employee.Mobile = txtMobile.Text;
                employee.LocationID = ddlLocation.SelectedIndex;
                employee.Salary = int.Parse(txtSalary.Text);
                employee.DeptID = ddlDepartment.SelectedIndex;

                ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Record Updated Successfully!!!')", true);

//Grid View

    protected void gvSearch_SelectedIndexChanged(object sender, EventArgs e)

            string ddlDept = gvSearch.SelectedRow.Cells[9].Text;
            string ddlLoc = gvSearch.SelectedRow.Cells[10].Text;

            ListItem deptRemove = ddlDepartment.Items.FindByText(ddlDept);
            ListItem locRemove = ddlLocation.Items.FindByText(ddlLoc);

            txtEmpID.Text = gvSearch.SelectedRow.Cells[1].Text;
            txtFirstName.Text = gvSearch.SelectedRow.Cells[2].Text;
            txtLastName.Text = gvSearch.SelectedRow.Cells[3].Text;
            txtMgrID.Text = gvSearch.SelectedRow.Cells[4].Text;
            txtDOB.Text = gvSearch.SelectedRow.Cells[5].Text;
            txtEmail.Text = gvSearch.SelectedRow.Cells[6].Text;
            txtMobile.Text = gvSearch.SelectedRow.Cells[7].Text;
            txtSalary.Text = gvSearch.SelectedRow.Cells[8].Text;
            ddlDepartment.Items.Insert(0, new ListItem(ddlDept));
            ddlLocation.Items.Insert(0, new ListItem(ddlLoc));



Related posts

Recent Viewed