停止代码在查询中执行

时间:2013-02-28 11:17:09

标签: c# asp.net linq-to-sql

我正在检查重复项并找到它们并且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();

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

在if语句中使用return;