Reputation: 59
I know that there are plenty of questions regarding this issue but I have not been able to solve it. I am doing a graphic interface to add/remove employee and manage absences.
I have been reading and testing those advices stackOverflow1 but I cannot change the database as it is already given by our professor.
The value from the foreign key is already in the table "service" that I called "department" on the code.
I have the following database (for the table on which I get issue):
DROP TABLE IF EXISTS personnel;
CREATE TABLE personnel (
IDPERSONNEL int NOT NULL,
IDSERVICE int NOT NULL,
NOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRENOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
TEL varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
MAIL varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS service;
CREATE TABLE service (
IDSERVICE int NOT NULL,
NOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In the application, that you can see here and on the picture just below, I have to modify an employee data and to save it again to the dataGridView.
I have an issue once I click on save. I have an error on my request but I don't know how to solve it.
Cannot add or update a child row: a foreign key constraint fails (gestionpersonnel
.personnel
, CONSTRAINT personnel_ibfk_1
FOREIGN KEY (IDSERVICE
) REFERENCES service
(IDSERVICE
))
Here is the method I have been using in the class AccessDataBase:
public static void UpdateEmployee(Employee employee)
{
string req = "update personnel set nom = @nom, prenom = @prenom, tel = @tel, mail = @mail, idservice = @idservice ";
req += "where idpersonnel = @idpersonnel;";
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{"@idpersonnel", employee.IdEmployee },
{"@idservice", employee.IdDepartment },
{"@nom", employee.FamilyName },
{"@prenom", employee.FirstName },
{"@tel", employee.Phone },
{"@mail", employee.Mail }
};
ConnexionDataBase connexion = ConnexionDataBase.GetInstance(connexionString);
connexion.ReqUpdate(req, parameters);
}
Update 1 :
I have been following the same order between the database and the code in C# for the request in the string variable.
Update 2 (reply to @Sharath N S), by using the step by step, I can get a value from the department
Update 3 (reply to @Crowcoder) : indeed that is the same comment @Sharath N S gave me. I have the feeling that I get the field of the department and not the idDepartment.
Here my code for the save button :
private void BtnSaveEmployee_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBoxFamilyName.Text) ||
String.IsNullOrEmpty(textBoxFirstName.Text) ||
String.IsNullOrEmpty(textBoxPhone.Text) ||
String.IsNullOrEmpty(textBoxMail.Text))
{
MessageBox.Show("All the informations shall be filled", "Information");
}
else
{
Department department = (Department)bindingSourceDepartments.List[bindingSourceDepartments.Position];
int idEmployee = 0;
if (modificationOngoing)
{
idEmployee = (int)dataGridViewEmployee.SelectedRows[0].Cells["idEmployee"].Value;
}
Employee employee = new Employee(idEmployee,
textBoxFamilyName.Text,
textBoxFirstName.Text,
textBoxPhone.Text,
textBoxMail.Text,
department.IdDepartment,
department.DepartmentName);
if (modificationOngoing)
{
controlMyApp.UpdateEmployee(employee);
modificationOngoing = false;
grpBoxEmployee.Enabled = true;
lblShowButtonClicked.Text = "Adding";
}
else
{
controlMyApp.AddEmployee(employee);
}
FillEmployeesList();
EmptyEmployeeSelection();
}
}
Upvotes: 0
Views: 193
Reputation: 106
Please check the value of IDservice that was being sent while updating the Employee details. From the error it looks like the IdService value that is being updated is not present in the parent table service.
Upvotes: 2