如何修复ASP> NET中生成报告“网络失败”时的错误

时间:2019-01-14 05:56:58

标签: c# asp.net

我有一个代码,可以从Report Viewer生成报告加载。我也有一个try catch异常,但它没有捕获任何错误。但是主要问题是“它可以生成报告但失败了”。它说“失败-网络错误”

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                List<ReportParameter> paramList = new List<ReportParameter>();
                DataTable dt = new DataTable();
                DataSet dsWeeklyReportView = new DataSet();

                string ConnectionString = "";

                string FileName = "WeeklyReport" + ".xls";
                string extension;
                string encoding;
                string mimeType;
                string[] streams;
                Warning[] warnings;


                string Name = Session["ProjectName"].ToString();
                string Status = Session["ProjectStatus"].ToString();
                string Active = Session["ProjectActive"].ToString();

                ReportViewer.ProcessingMode = ProcessingMode.Local;
                paramList.Add(new ReportParameter("WEEKLYREPORTNAME", Session["ProjectName"].ToString()));
                paramList.Add(new ReportParameter("WEEKLYREPORTSTATUS", Session["ProjectStatus"].ToString()));
                paramList.Add(new ReportParameter("WEEKLYREPORTACTIVE", Session["ProjectActive"].ToString()));

                ReportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;

                ConnectionString = "DataSet";

                ReportViewer.LocalReport.ReportPath = Server.MapPath("~") + "ReportGenerator.rdl";

                dsWeeklyReportView = oWeeklyReport.dsWeeklyReport(Name, Status, Active);
                dt = dsWeeklyReportView.Tables[0];
                ReportViewer.LocalReport.DataSources.Add(new ReportDataSource(ConnectionString, dt));
                ReportViewer.LocalReport.SetParameters(paramList);
                ReportViewer.LocalReport.Refresh();
                // ReportViewer2.Visible = true;
                Byte[] mybytes = ReportViewer.LocalReport.Render("EXCEL", null,
                        out extension, out encoding,
                        out mimeType, out streams, out warnings); //for exporting to PDF  
                using (FileStream fs = File.Create(Server.MapPath("~//download//") + FileName))
                {
                    fs.Write(mybytes, 0, mybytes.Length);
                }

                Response.ClearHeaders();
                Response.ClearContent();
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/xls";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
                Response.WriteFile(Server.MapPath("~/download/" + FileName));
                Response.Flush();
                Response.Close();
                Response.End();

            }
            catch (Exception ex)
            { }
        }
    }

我已经检查了参数,路径和名称。尝试捕获没有错误,它会生成报告,但失败。谢谢您的回答。

1 个答案:

答案 0 :(得分:0)

经过一番调查,我发现总是调用Response.End()Response.Clear()引发异常,方法是调用:

HttpContext.Current.Response.End();
HttpContext.Current.Response.Clear();