2013-06-19 45 views
0

我正在一個項目中,我從一個excel文件中讀取多個字段並將它們保存在db中。如果用戶輸入錯誤的值,如何在頁面上顯示多個錯誤消息。 Aspose.cells用於讀取數據。我的代碼是如何在不驗證的情況下在頁面上顯示錯誤摘要?

public List<Data> ImportFromExcel(Stream bytes, out bool isFine) 
    { 
     isFine = true; 
     DateTime DOJ; 

     List<Data> list = new List<Data>(); 
     DataTable dt = new DataTable(); 

     Workbook workBook = new Workbook(); 
     workBook.Open(bytes); 

     Worksheet workSheet = workBook.Worksheets[0]; 
     try 
     { 
      dt = workSheet.Cells.ExportDataTable(0, 0, workSheet.Cells.MaxRow + 1, workSheet.Cells.MaxColumn + 1, true); 

     } 
     catch (Exception ex) 
     { 
      isFine = false; 
      ShowMessage("Your file has some invalid formats of data. Please review it and try again.", MessageType.Error, true); 
      return null; 
     } 

     try 
     { 
      int i = 1; 

      foreach (DataRow reader in dt.Rows) 
      { 
       if (reader["LetterId"].ToString().Length > 75) 
       { 
        isFine = false; 
        ShowMessage("In Row Number " + i + " Letter Id cannot exceed 75 characters.", MessageType.Error, true); 
        return null; 
       } 
       if (reader["Subject"].ToString().Length > 75) 
       { 
        isFine = false; 
        ShowMessage("In Row Number " + i + " Subject cannot exceed 75 characters.", MessageType.Error, true); 
        return null; 
       } 
       . 
       . 
       . 

顯示消息方法只顯示單個錯誤消息。

+0

建議:調用'ShowMessage'前'變量在'列表添加你的錯誤消息。最後,結合錯誤消息('string.Join')並調用'ShowMessage' – mshsayem

回答

0

您可以創建一個空的字符串列表,然後每次遇到錯誤時將其添加到列表中。然後,一旦你在你的列表中的所有錯誤,你可以這樣做:

 string allErrors = string.Empty; 
     foreach (string err in errorList) 
     { 
      allErrors += err + "<br />"; 
     } 
     if (allErrors != string.Empty) 
     { 
      ShowMessage(allErrors); 
     } 
相關問題