返回首页

SQL错误:ID已经存在!|大家好!
在我下面的C#代码的记录添加到数据库中。错误消息说,该部门ID已经存在。

        

        private void btnAdd_Click(object sender, EventArgs e)

        {

            SqlDataAdapter adp = new SqlDataAdapter("Select * from Sector", con);

            DataSet ds = new DataSet();

            adp.Fill(ds, "Sector");

            if (txtSectorID.Text == "")

            {

                MessageBox.Show("Please Enter Sector ID");

                txtSectorID.Focus();

                return;

            }

            if (txtDesc.Text == "")

            {

                MessageBox.Show("Please Enter some Description");

                txtDesc.Focus();

                return;

            }

            else if (txtFCFares.Text == "")

            {

                MessageBox.Show("Please Enter First Class Fares");

                txtFCFares.Focus();

                return;

            }

            if (!(txtFCFares.Text == ""))

            {

                string fstr;

                string fstr1 = null;

                bool fcheck = false;

                int fi = 0;

                int fasci = 0;

                fstr = txtFCFares.Text;

                for (fi = 1; fi <= fstr.Length; fi++)

                {

                    fstr1 = fstr.Substring(fi - 1, 1);

                    fasci = System.Convert.ToInt32(fstr1[0]);

                    if (fasci >= 48 & fasci <= 57)

                    {

                        fcheck = true;

                    }

                    else

                    {

                        fcheck = false;

                        break;

                    }

                }

                if (fcheck == false)

                {

                    MessageBox.Show("Enter valid Fares");

                    txtFCFares.Text = "";

                    txtFCFares.Focus();

                    return;

                }

            }

            if (txtBCFares.Text == "")

            {

                MessageBox.Show("Please Enter Business Class Fares");

                txtBCFares.Focus();

                return;

            }

            if (!(txtBCFares.Text == ""))

            {

                string bstr;

                string bstr1 = null;

                bool bcheck = false;

                int bi = 0;

                int basci = 0;

                bstr = txtBCFares.Text;

                for (bi = 1; bi <= bstr.Length; bi++)

                {

                    bstr1 = bstr.Substring(bi - 1, 1);

                    basci = System.Convert.ToInt32(bstr1[0]);

                    if (basci >= 48 & basci <= 57)

                    {

                        bcheck = true;

                    }

                    else

                    {

                        bcheck = false;

                        break;

                    }

                }

                if (bcheck == false)

                { 

                    MessageBox.Show("Enter Valid Fares");

                    txtBCFares.Focus();

                    return;

                }

            }

            if (txtECFares.Text == "")

            {

                MessageBox.Show("Please Enter Economy Class Fares");

                txtECFares.Focus();

                return;

            }

            if (!(txtECFares.Text == ""))

            {

                string estr;

                string estr1 = null;

                bool echeck = false;

                int ei = 0;

                int easci = 0;

                estr = txtECFares.Text;

                for (ei = 1; ei <= estr.Length; ei++)

                {

                    estr1 = estr.Substring(ei - 1, 1);

                    easci = System.Convert.ToInt32(estr1[0]);

                    if (easci >= 48 & easci <= 57)

                    {

                        echeck = true;

                    }

                    else

                    {

                        echeck = false;

                        break;

                    }

                }

                if (echeck == false)

                {

                    MessageBox.Show("Enter Valid Fares");

                    txtECFares.Focus();

                    return;

                }

            }

            try

            {

                table = ds.Tables["Sector"];

                row = table.NewRow();

                row[0] = txtSectorID.Text;

                row[1] = txtDesc.Text;

                row[2] = txtFCFares.Text;

                row[3] = txtBCFares.Text;

                row[4] = txtECFares.Text;

                table.Rows.Add(row);

                SqlCommandBuilder comm = new SqlCommandBuilder(adp);

                adp.InsertCommand = comm.GetInsertCommand();

                adp.Update(ds, "Sector");

                adp.Fill(ds);

                MessageBox.Show("New Entry Saved");

                SqlDataAdapter adp2 = new SqlDataAdapter("Select * from Sector", con);

                DataSet ds2 = new DataSet();

                adp.Fill(ds2, "Sector");

                dgvSectorDetails.DataSource = ds2.Tables[0].DefaultView;

                txtSectorID.Text = "";

                txtDesc.Text = "";

                txtFCFares.Text = "";

                txtBCFares.Text = "";

                txtECFares.Text = "";

            }

            catch

            {

                MessageBox.Show("Sector ID already Exist");

            }

        }


任何人都可以,请告诉错误是怎么回事吗?

感谢您的考虑!

回答

评论会员:游客 时间:2012/02/06
曼弗雷德・R.Bihy:你在你的代码有点残酷。在您尝试catch语句,你清楚地假设,如果出现异常,那一定是因为该部门ID已经存在。这是如何做到这一点。如果一个异常,预计使用正确的catch块。如果您使用多个catch块,确保正在捕获的异常是从最具体到最不特定的顺序排列。{C}你研究的一些东西:imgsrc=的问候,mdash;如MRB
曼弗雷德・R. Bihy
评论会员:{的C2} 时间:2012/02/06