1

我正在检查重复项,它发现它们并且 If(isDuplicate) 一切正常。但是代码继续执行所有提交,因为我没有告诉它停止。我该怎么做?停止并抛出它确实但未提交的错误面板?

_db.tbl_Localities.InsertOnSubmit(locality);

bool isDuplicate = _db.tbl_Localities
                    .Any(x => x.Locality == txt_Locality.Text);
if (isDuplicate)
{
    pnl_Message.Visible = true;
    lbl_message.Text = " Duplicate entry!";
    txt_Locality.Text = "";
}

// Save
// ====
_db.SubmitChanges();
4

2 回答 2

3

您应该处理else部分条件,或者如果您的程序出现意外情况,则引发异常:

_db.tbl_Localities.InsertOnSubmit(locality);

bool isDuplicate = _db.tbl_Localities
                    .Any(x => x.Locality == txt_Locality.Text);
if (isDuplicate)
{
    pnl_Message.Visible = true;
    lbl_message.Text = " Duplicate entry!";
    txt_Locality.Text = "";
}
else
{
    // Save
    // ====
    _db.SubmitChanges();
}
于 2013-02-28T11:19:25.287 回答
0

return;在 if 语句中使用。

于 2013-02-28T11:19:32.207 回答