1

How to dispose a crystal report while closing the tab or page? This is the way i have implemented report viewer. Can any one suggest a proper way to close and dispose report.

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());
    }

}

EDIT: Is there any way to dispose reports while closing the tab or page? If i try to dispose report in Page_Unload like this

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

Then it is not possible to navigate to other pages of report. So Can anyone suggest a proper way of invoking Close() and Dispose() methods?

Edit: There is another question that reads similar, but it is actually different, because that qusetion explains what is null reference exception and how to fix it. But what i wanted is how to close and dispose a crytal report while closing tab or page.

Spider man
  • 3,006
  • 4
  • 25
  • 41
  • Where are you making the call to `rd.Close();` and `rd.Dispose();`? Wherever it is, the object is null (it might be out of scope) already. – Tim Oct 20 '14 at 05:45
  • Did you ever find a solution to this? – ngm Nov 04 '15 at 14:28

1 Answers1

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);
        }

Your code

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();
       }

    }
Sumon Banerjee
  • 1,705
  • 4
  • 23
  • 39
  • Thank you Sumon. But after implementing this the reports are displaying an error."System.NullReferenceException: Object reference not set to an instance of an object." because Dispose() is called in Page_Init. Is there any way to call Dispose() while closing the tab or page? – Spider man Oct 21 '14 at 10:01