1

我有以下问题:

我尝试插入multiple employee data,但是员工数据应该只插入一次,但是如果我选择多个员工并且之前已经插入了其中一个,根据数据库约束会抛出错误。

我的问题是(最佳实践):如何处理这个异常,我想恢复插入未重复的员工而不退出我的循环,因为那个异常。


protected void ibtn_save_detail_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Fill_Form();
                RewardDetails obj = new RewardDetails();
                var collection = ddl_employees.CheckedItems;
                for (int i = 0; i < collection.Count; i++)
                {
                    obj.Emp_num = int.Parse(collection[i].Value);
                    obj.Req_ser = int.Parse(reqSer);
                    obj.Req_year = int.Parse(reqYear);
                    DataTable dt = Utilities.GetDep(obj.Emp_num);
                    obj.Main_code = int.Parse(dt.Rows[0]["dep_code"].ToString());
                    obj.Year = int.Parse(dt.Rows[0]["dep_year"].ToString());
                    obj.Dep_name = dt.Rows[0]["dep_name"].ToString();

                    string res = obj.InsertReward();//exception in case of repetition .

                    if (!string.IsNullOrEmpty(res))
                    {

                        div_detail_result.Visible = true;
                        SetMessage("");

                    }
                    else
                    {
                        SetMessage("Adding the employee has been done :" + collection[i].Text.Trim());
                    }
                }
                BindDetailsGV(obj.Req_ser, obj.Req_year);
                ddl_employees.ClearCheckedItems();

            }
            catch (Exception ee)
            {
                SetMessage("Error,this employee has been added before.");
                ddl_employees.ClearCheckedItems();
            }
        }
4

4 回答 4

1

移动您的 try/catch 以仅应用于该obj.InsertReward();行。另外,不要捕获Exception,只是抛出的特定异常insertReward

所以代码可能最终会是这样的:

protected void ibtn_save_detail_Click(object sender, ImageClickEventArgs e)
{
    Fill_Form();
    RewardDetails obj = new RewardDetails();
    var collection = ddl_employees.CheckedItems;
    for (int i = 0; i < collection.Count; i++)
    {
        obj.Emp_num = int.Parse(collection[i].Value);
        ...

        string res;
        try
        {
            res = obj.InsertReward();
            // stuff to do if added
        }
        catch (RewardExistsException)
        {
            // stuff to do if already exists
        }
    }
}
于 2013-10-21T11:13:26.917 回答
1

您应该放入try循环中:

for (int i = 0; i < collection.Count; i++)
{
    try
            {
                obj.Emp_num = int.Parse(collection[i].Value);
                obj.Req_ser = int.Parse(reqSer);
                obj.Req_year = int.Parse(reqYear);
                DataTable dt = Utilities.GetDep(obj.Emp_num);
                obj.Main_code = int.Parse(dt.Rows[0]["dep_code"].ToString());
                obj.Year = int.Parse(dt.Rows[0]["dep_year"].ToString());
                obj.Dep_name = dt.Rows[0]["dep_name"].ToString();

                string res = obj.InsertReward();//exception in case of repetition .

                if (!string.IsNullOrEmpty(res))
                {

                    div_detail_result.Visible = true;
                    SetMessage("");

                }
                else
                {
                    SetMessage("Adding the employee has been done :" + collection[i].Text.Trim());
                }
            }
    catch (Exception ee)
        {
            // your exception code
        }
}
于 2013-10-21T11:15:07.207 回答
1
try{
string res = obj.InsertReward();
}
catch(Exception ex){
    continue; // or rethrow exception in some condions
}

并且不要忘记在填充 obj 的过程中可能会出现异常

于 2013-10-21T11:15:51.517 回答
1

make sure your try catch does not encapsulate the for loop so:

try
{
   for(...){}
}
catch(Exception){  }

should be:

for(...)
{
    try{  }
    Catch{  }
}

or if the exception is not thrown inside the for loop simply catch before you start the loop or try after you start the loop.

于 2013-10-21T11:17:11.437 回答