简体   繁体   English

C# - 无法添加或更新子行:外键约束失败

[英]C# - Cannot add or update a child row: a foreign key constraint fails

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.我一直在阅读和测试这些建议stackOverflow1但我无法更改数据库,因为它已经由我们的教授给出。

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.在应用程序中,您可以在此处和下图看到,我必须修改员工数据并将其再次保存到 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 ))无法添加或更新子行:外键约束失败( gestionpersonnel personnel CONSTRAINT personnel_ibfk_1 FOREIGN KEY ( IDSERVICE ) REFERENCES service ( IDSERVICE ))

Here is the method I have been using in the class AccessDataBase:这是我在 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:更新1:

在此处输入图像描述 I have been following the same order between the database and the code in C# for the request in the string variable.对于字符串变量中的请求,我一直在数据库和 C# 中的代码之间遵循相同的顺序。

应用程序的屏幕截图

Update 2 (reply to @Sharath NS), by using the step by step, I can get a value from the department更新2(回复@Sharath NS),通过使用一步一步,我可以从部门得到一个值在此处输入图像描述

Update 3 (reply to @Crowcoder): indeed that is the same comment @Sharath NS gave me.更新 3(回复@Crowcoder):确实是@Sharath NS 给我的相同评论。 I have the feeling that I get the field of the department and not the idDepartment.我有一种感觉,我得到了部门的领域,而不是 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();
            }
        }

在此处输入图像描述

Please check the value of IDservice that was being sent while updating the Employee details.请检查更新员工详细信息时发送的 IDservice 的值。 From the error it looks like the IdService value that is being updated is not present in the parent table service.从错误看来,父表服务中不存在正在更新的 IdService 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails “无法添加或更新子行:外键约束失败”错误 - 'Cannot add or update a child row: a foreign key constraint fails' Error 错误-无法添加或更新子行:外键约束失败 - Error - Cannot add or update a child row: a foreign key constraint fails C#:在数据库中添加对象时,出现“ MySqlException:无法添加或更新子行:外键约束失败” - C#: I'm getting a “MySqlException: Cannot add or update a child row: a foreign key constraint fails” when i am Adding an object in my database EF6代码优先:MySqlException:无法添加或更新子行:外键约束失败 - EF6 Code First: MySqlException: Cannot add or update a child row: a foreign key constraint fails ASP.NET:无法添加或更新子行:外键约束失败 - ASP.NET: Cannot add or update child row: a foreign key constraint fails ASP.NET MVC 5 EF无法添加或更新子行:外键约束失败 - ASP.NET MVC 5 EF Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:在 foreach 中执行 INSERT 时外键约束失败 - Cannot add or update a child row: a foreign key constraint fails while doing INSERT in a foreach C#FrameworkCore和MySQL:无法删除或更新父行:外键约束失败 - C# FrameworkCore and MySQL: Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM