使用c#foreach语句

时间:2016-06-14 00:49:23

标签: c# asp.net 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
}

5 个答案:

答案 0 :(得分:0)

试试这个:

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);
    }
}

答案 1 :(得分:0)

我建议您将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,而不是这样做,您可以将foreach包含在try块中。因此,如果有任何异常,循环将自动中断并执行catch。

答案 2 :(得分:0)

这应该可以解决问题。

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语句中。

答案 3 :(得分:0)

return关键字用于终止循环。

我会使用boolean变量检查successfailure

的状态
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();

答案 4 :(得分:0)

我想如果你想从.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;
    }    
}