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.