如何在没有验证的情况下在页面上显示错误摘要?

时间:2013-06-19 08:09:32

标签: c# asp.net

我正在开发一个项目,在该项目中,我从excel文件中读取多个字段并将其保存在db.How中,以便在用户输入错误值时在页面上显示多个错误消息。 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;
                }
                 .
                 .
                 .

显示消息方法仅显示单个错误消息。

1 个答案:

答案 0 :(得分:0)

您可以创建一个空字符串列表,然后每次遇到错误时只需将其添加到列表中即可。然后,一旦列出了所有错误,就可以执行此操作:

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