我有以下问题:
我尝试插入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();
}
}