2016-06-14 78 views
0

我正在使用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 
} 

回答

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

asp.net?的MessageBox.show? –

+0

對不起,我沒有注意。現在修復。我認爲這個答案*現在*符合您的要求,因爲只會顯示*一條*信息。 –

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 in e ach迭代,而不是你可以把foreach放在try塊內。因此,如果有任何異常,該循環將自動中斷並執行捕獲。

+0

asp.net?的MessageBox.show? –

+0

@ Dr.Stitch:很好,感謝您指出錯誤 –

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語句中包含整個過程。

+0

即使出現錯誤,我仍然會收到成功消息(爲了檢查,我在更新前停止了數據庫服務) – DavidM

+0

兩者都會警告,但是會顯示不同的消息。 –

+0

我更新答案請嘗試。 –

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(); 
+0

即使出現錯誤,我仍然會收到成功消息(爲了檢查,我在更新之前停止數據庫服務) – DavidM

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; 
    }  
} 
+0

asp.net?的MessageBox.show? –