关闭标签或页面时如何处理水晶报告?

时间:2014-10-20 05:36:21

标签: c# asp.net crystal-reports

如何在关闭标签或页面时处理水晶报告? 这是我实现报表查看器的方式。任何人都可以建议一种合适的方式来关闭和处理报告。

protected void Page_Init(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            bool isValid = true;

            // Setting ReportName
            string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();

            // Setting Report Data Source     
            var rptSource = System.Web.HttpContext.Current.Session["rptSource"];

            if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
            {
                isValid = false;
            }


            if (isValid) // If Report Name provided then do other operation
            {
                rd = new ReportDocument();
                if (Session["ReportDocument"] != null)
                {
                    rd = Session["ReportDocument"] as ReportDocument;
                    rd.Load(strReportName);

                    CrystalReportViewer1.ReportSource = rd;
                }
                else
                {                        
                    string stringReportPath = strReportName;
                    //Loading Report
                    rd.Load(stringReportPath);

                    // Setting report data source
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);
                    Session["ReportDocument"] = rd;

                    CrystalReportViewer1.ReportSource = rd;
                }
                Session["ReportName"] = "";
                Session["rptSource"] = "";
            }
            else
            {
                Response.Write("<H2>Nothing Found; No Report name found</H2>");
            }
        }
        else
        {
            ReportDocument doc = (ReportDocument)Session["ReportDocument"];
            CrystalReportViewer1.ReportSource = doc;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }

}

编辑: 有没有办法在关闭标签或页面时处理报告? 如果我尝试像这样在Page_Unload中处理报告

protected void Page_Unload(object sender, EventArgs e)
{
if (rd != null)
 {
   rd.Close();
   rd.Dispose();
 }
}

然后无法导航到报告的其他页面。那么有人能建议一种调用Close()和Dispose()方法的正确方法吗?

编辑:还有另一个类似的问题,但它实际上是不同的,因为该问题解释了什么是空引用异常以及如何修复它。但我想要的是如何在关闭标签或页面时关闭和处理crytal报告。

1 个答案:

答案 0 :(得分:0)

        private bool disposed = false;
       ReportDocument doc=new ReportDocument ();
       ReportDocument rd=new ReportDocument ();
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    doc.Dispose(); //context means your crystal report document object.
                    rd.Dispose(); 
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

您的代码

protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                bool isValid = true;

                // Setting ReportName
                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();

                // Setting Report Data Source     
                var rptSource = System.Web.HttpContext.Current.Session["rptSource"];

                if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
                {
                    isValid = false;
                }


                if (isValid) // If Report Name provided then do other operation
                {
                    rd = new ReportDocument();
                    if (Session["ReportDocument"] != null)
                    {
                        rd = Session["ReportDocument"] as ReportDocument;
                        rd.Load(strReportName);

                        CrystalReportViewer1.ReportSource = rd;
                    }
                    else
                    {                        
                        string stringReportPath = strReportName;
                        //Loading Report
                        rd.Load(stringReportPath);

                        // Setting report data source
                        if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                            rd.SetDataSource(rptSource);
                        Session["ReportDocument"] = rd;

                        CrystalReportViewer1.ReportSource = rd;
                    }
                    Session["ReportName"] = "";
                    Session["rptSource"] = "";
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            else
            {
                doc=new ReportDocument();
                doc = (ReportDocument)Session["ReportDocument"];
                CrystalReportViewer1.ReportSource = doc;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }


     finally
       {
         Dispose();
       }

    }