update语句与外键约束“fk_auto_part_details_hand_installation”冲突。冲突发生在数据库“db”,表“dbo.hand_installation”列中 [英] The update statement conflicted with the foreign key constraint "fk_auto_part_details_hand_installation". the conflict occurred in database "db", table "dbo.hand_installation", column

查看:49
本文介绍了update语句与外键约束“fk_auto_part_details_hand_installation”冲突。冲突发生在数据库“db”,表“dbo.hand_installation”列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btn_Edit_Click(object sender, EventArgs e)
       {
             using (sd = new SqlCommand("update Auto_Part_Details set Value_Part= @Value_Part, Auto_Parts_id = @Auto_Parts_id, Repair_id = @Repair_id, New_Old_Part = @New_Old_Part ,Provider_id = @Provider_id , Installation_id = @Installation_id , Notes = @Notes where Job_Card = @Job_Card and Items_No = @Items_No", con))
                {
                    sd.Parameters.AddWithValue("@Items_No", txt_Items_No.Text);
                    sd.Parameters.AddWithValue("@Job_Card", txt_Job_card.Text);
                    sd.Parameters.AddWithValue("@Value_Part", txt_Value_Part.Text);
                    sd.Parameters.AddWithValue("@Auto_Parts_id",
                    Convert.ToInt32(CB_Auto_parts.SelectedValue));
                    sd.Parameters.AddWithValue("@Repair_id",
                    Convert.ToInt32(CB_Repair_type.SelectedValue));
                    sd.Parameters.AddWithValue("@New_Old_Part", Convert.ToBoolean(x));
                    sd.Parameters.AddWithValue("@Provider_id",
                    Convert.ToInt32(CB_Providers_desc.SelectedValue));
                    sd.Parameters.AddWithValue("@Installation_id",
                    Convert.ToInt32(CB_Hand_installation.SelectedValue));
                    sd.Parameters.AddWithValue("@Notes", txt_Notes.Text);
                }
       }





我的尝试:



当这些组合框值之一为null时,如果我能解决这个问题就会发生这个错误



What I have tried:

this error happen when one of these combobox value is null how i can solve this problem

推荐答案

错误信息很漂亮clear:您的更新与外键条件冲突。即更新的结果将是另一个表中不存在的ID值 - 因此您的数据将不一致,SQL将不允许这样做。



最好 - 可能只是现实 - 的解决方案是在开始更新之前仔细检查用户输入。确保数字是数字,选择ID值。如果不是,那么您使用用户提供的数据执行的任何其他操作将在以后给您带来严重困难。告诉用户问题是什么并让他现在解决它很简单,但是一个真正的PITA可以在你的数据库充满部分条目和垃圾的六个月内解决问题!
The error message is pretty clear: your update conflicts with a foreign key condition. I.e. the result of the update would be an ID value that doesn't exist in the other table - so your data would be inconsistent and SQL won't allow that.

The best - probably only realistic - solution would be to check your user inputs carefully before you start to do the update. Make sure the numbers are numbers, the ID values are selected. If they aren't, then any other action you take with the user supplied data is going to give you serious difficulties later. It's simple to tell the user what the problem is and get him to fix it now, but a real PITA to sort out the problems in six months time when your DB is full of partial entries and rubbish!


显然,您的外键列不允许空值。如果这仍然是你想要的,你必须在尝试插入它们之前检查这些值是否为null,而是给用户一个错误消息,指出他需要选择一些值。如果您确实想要插入空值(这意味着您在设置数据库模式后重新考虑),则更改数据库中的相应外键列以允许空值。
Apparently your foreign key column(s) doesn't allow nulls. If that's still what you want you have to check for those values being null before attempting to insert them and instead give the user an error message stating that he needs to select some value. If you actually do want to insert nulls (which would mean you reconsidered since you've set up your database schema) then change the appropriate foreign key columns in the database to allow nulls.


这篇关于update语句与外键约束“fk_auto_part_details_hand_installation”冲突。冲突发生在数据库“db”,表“dbo.hand_installation”列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆