cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to close the DB connections after the page is rendered?

Former Member
0 Kudos

I have a .NET Crystal Reports project that allows me to view reports from within my main program. This works nicely in general, but when the viewer is open it keeps a connection to to database object and further changes to the database are disallowed. What I would like to happen is for the report to take a snapshot of the data and ignore any new changes. It will need to close the active connection to allow modifications to take place. Are there settings that I can change to make the report object open the connection, read the data, then discard the connection? Is there an explicit way to do what I ask after the report source is passed to the viewer?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

In that case you can use the disconnected architecture of database connection i.e. achieved by using dataset.

Take help from sample code click [here|https://boc.sdn.sap.com/codesamples].

You can also take help from [Dev library|https://boc.sdn.sap.com/node/7770]

Hope this helps!!

Regards,

Amit

Answers (1)

Answers (1)

former_member208657
Active Contributor
0 Kudos

Another alternative is to export the Crystal Report to the .rpt format. Once you've exported to .rpt format it is treated as a saved data report. You can then load this saved data report on each postback conserve your database connection and print jobs.

- Keep in mind that this will add overhead to the first time you attempt to preview the report because the who report needs to be generated.

- You also need to keep track of all your .rpt files you export and clean up once in a while. Otherwise you run the risk of filling your temp directory with temp .rpt files.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


namespace bo11_conserve_printjobs
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
		protected System.Web.UI.WebControls.Button Button1;
		private ReportDocument boReportDocument;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here

			if(!this.IsPostBack)
			{
				// This is the first time the page has been loaded. Code here 
				// will handle all database and parameter code; then export the
				// report to Crystal Reports .rpt format.
				boReportDocument = new ReportDocument();
				boReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));

				#region Export Code
				ExportOptions boExportOptions = new ExportOptions();
				DiskFileDestinationOptions boDiskFileDestinationOptions = ExportOptions.CreateDiskFileDestinationOptions();

				// Set the DiskFileName to a random guid in the windows\temp directory,
				// and store the name as a string in Session.
				// This value can be stored as a querystring, form variable, viewstate, etc.
				boDiskFileDestinationOptions.DiskFileName = @"c:\windows\temp\" + Guid.NewGuid().ToString() + ".rpt";				
				boExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
				boExportOptions.ExportDestinationOptions = boDiskFileDestinationOptions;
				Session.Add("ReportFilename", boDiskFileDestinationOptions.DiskFileName);

				// 
				boExportOptions.ExportFormatType = ExportFormatType.CrystalReport;

				boReportDocument.Export(boExportOptions);	
				#endregion			
			}
			else
			{
				string filename = (string) Session["ReportFilename"];

				boReportDocument = new ReportDocument();
				boReportDocument.Load(filename);
			}

			CrystalReportViewer1.ReportSource = boReportDocument;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Unload += new System.EventHandler(this.WebForm1_Unload);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			// Remove the file from the windows\temp directory and 
			// redirect to the final page.
			string filename = (string) Session["ReportFileName"];
			System.IO.File.Delete(filename);

		
		}

		private void WebForm1_Unload(object sender, System.EventArgs e)
		{
			// Clean up code for Crystal Reports
			if(boReportDocument != null)
			{
				boReportDocument.Close();
				boReportDocument.Dispose();
				boReportDocument =  null;
			}

			Response.Redirect("final.aspx");
		}
	}
}