使用c#foreach语句时显示错误或成功消息
我正在使用C#。如果所有数据都已提交,我该如何显示成功消息,或者在foreach循环期间发生错误时是否显示错误消息并中断循环?使用c#foreach语句时显示错误或成功消息
这是我的尝试,但我不知道要显示的邮件的最好办法:
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataDTO data = new DataDTO();
BaseDAO connexion = new BaseDAO();
foreach (GridViewRow row in this.GridView1.Rows)
{
data.lblId = ((Label)row.FindControl("lblId")).Text;
try
{
connexion.update_database(data);
}
catch
{
// Display error message and break loop
}
}
GridView1.EditIndex = -1;
LoadGrid();
// Display success message
}
试试这个:
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataDTO data = new DataDTO();
BaseDAO connexion = new BaseDAO();
bool success = true;
foreach (GridViewRow row in this.GridView1.Rows)
{
data.lblId = ((Label)row.FindControl("lblId")).Text;
try
{
connexion.update_database(data);
}
catch
{
// Display error message and break loop
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('<success message>')", true);
success = false;
break;
}
}
GridView1.EditIndex = -1;
LoadGrid();
// Display success message
if (success)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('<success message>')", true);
}
}
我想建议你到封闭foreach
在try..catch里面,如下所示:
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataDTO data = new DataDTO();
BaseDAO connexion = new BaseDAO();
try
{
foreach (GridViewRow row in this.GridView1.Rows)
{
// Perform operations here
}
// show success message
}
catch
{
// Show Some Error message
isError=true;
}
// Rest of process
}
您不需要使用try..catch in e ach迭代,而不是你可以把foreach放在try块内。因此,如果有任何异常,该循环将自动中断并执行捕获。
asp.net?的MessageBox.show? –
@ Dr.Stitch:很好,感谢您指出错误 –
这应该可以做到。
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataDTO data = new DataDTO();
BaseDAO connexion = new BaseDAO();
try
{
foreach (GridViewRow row in this.GridView1.Rows)
{
data.lblId = ((Label)row.FindControl("lblId")).Text;
connexion.update_database(data);
}
GridView1.EditIndex = -1;
LoadGrid();
// Display success message
// Success Alert
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Update Successfully')", true);
}
catch (Exception ex)
{
// Display error message and break loop
// Error Alert
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", string.Format("alert('Record Update Failed: {0}')", ex.Message), true);
}
}
我建议你在try-catch语句中包含整个过程。
return
关键字用于终止循环。
我将采取boolean
变量来检查状态无论是success
或failure
bool isSuccess = true;
StringBuilder message = new StringBuilder();
foreach (GridViewRow row in this.GridView1.Rows)
{
data.lblId = ((Label)row.FindControl("lblId")).Text;
try
{
connexion.update_database(data);
}
catch
{
isSuccess = false;
// Display error message and break loop
return;
}
}
if(!isSuccess)
//Show success message
GridView1.EditIndex = -1;
LoadGrid();
即使出现错误,我仍然会收到成功消息(为了检查,我在更新之前停止数据库服务) – DavidM
,如果你想显示错误的按摩从.NET
您必须添加一些异常,然后引发错误消息,我想。
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataDTO data = new DataDTO();
BaseDAO connexion = new BaseDAO();
try
{
foreach (GridViewRow row in this.GridView1.Rows)
{
data.lblId = ((Label)row.FindControl("lblId")).Text;
connexion.update_database(data);
}
}
catch(Exception exc)
{
MessageBox.Show(exc.Message); //message from .NET
//MessageBox.Show("your custom message"); // custom message.
isError=true;
}
}
asp.net?的MessageBox.show? –
asp.net?的MessageBox.show? –
对不起,我没有注意。现在修复。我认为这个答案*现在*符合您的要求,因为只会显示*一条*信息。 –