Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列为主键时输入重复项时显示错误消息_C# - Fatal编程技术网

C# 在列为主键时输入重复项时显示错误消息

C# 在列为主键时输入重复项时显示错误消息,c#,C#,由于在名为Emp的表中,我有一个名称列是主键,当我添加重复的名称(如Bharat Mali)时,该记录不会添加到数据库中,但会显示该消息(成功添加员工),我想向用户显示一条消息,如(无法存储重复值)。 我在添加按钮单击后使用此代码 private void button3_Click(object sender, EventArgs e) { DBHandling db = new DBHandling(); if (name.Te

由于在名为Emp的表中,我有一个名称列是主键,当我添加重复的名称(如Bharat Mali)时,该记录不会添加到数据库中,但会显示该消息(成功添加员工),我想向用户显示一条消息,如(无法存储重复值)。 我在添加按钮单击后使用此代码

private void button3_Click(object sender, EventArgs e)
        {
            DBHandling db = new DBHandling();
            if (name.Text.Trim().Equals("")&& name.Text.Trim().Equals("select Emp.name from Emp"))
            {
                name.Focus();
                MessageBox.Show("Name cannot be blank or same!");
            }
            else if(Payment.Text.Trim().Equals(""))
            {
                Payment.Focus();
                MessageBox.Show("Payment cannot be blank!");
            }
            else if (!IsNumeric(Payment.Text.Trim()))
            {
                Payment.Focus();
                MessageBox.Show("Payment should contain Numerical Values!");
            }
                else if(CellNumber.Text.Trim().Equals(""))
            {
                CellNumber.Focus();
                MessageBox.Show("Cell Number cannot be blank!");

            }
            else if (!IsNumeric(CellNumber.Text.Trim()) || CellNumber.Text.Trim().Length<10)
            {
                CellNumber.Focus();
                MessageBox.Show("Invalid Cell Number!");
            }
            else if(JoiningDate.Text.Trim().Equals(""))
            {
                JoiningDate.Focus();
                MessageBox.Show("set Joining Date!");
            }             
            else
            {
                db.AddEmp(name.Text, Payment.Text, CellNumber.Text, Education.Text, JoiningDate.Text);
                MessageBox.Show("Employee added successfully!");
            }
            name.Clear();           
            Payment.Clear();           
            CellNumber.Clear();            
            Education.Clear();
            name.Focus();

        }

编写代码的异常方式不好。它会降低应用程序的性能。而是检查数据库中是否已经存在PK值。可能是为了加快速度,您可以只使用PKs的内部数据结构,并首先检查该结构,如果不存在,则更新该结构加DB

是否尝试捕获由
DB.AddEmp
方法引发的异常或返回值?您应该从基础数据库中获得错误。使db.AddEmp成为一个函数,并返回错误代码或一个布尔值,说明它已成功。查看db.AddEmp.if(name.Text.Trim().Equals(“”&&name.Text.Trim().Equals(“从Emp中选择Emp.name”){name.Focus();MessageBox.Show(“名称不能为空或相同!”);}的代码会很有帮助我正在用这行代码检查PK,我没有得到任何答案,但甚至没有错误,你知道我应该怎么做吗?请正确缩进代码。这样读代码很难。也许你可以使用pastebin.com我已经在我的问题结束时编辑了这行代码,请检查一下
if (name.Text.Trim().Equals("")&& name.Text.Trim().Equals("select Emp.name from Emp"))
                {
                    name.Focus();
                    MessageBox.Show("Name cannot be blank or same!");
                }