CrystalToHttpResponse Crystal report without running CRViewer

So, I am trying to export a crystal report in pdf without opening the report in the runtime viewer of my web application using

ExportToHttpResponse

method. Everything seems to work correctly when it comes to loading parameters, getting the file / path to load the report. But when I execute the part that is supposed to create a popup dialog box that gives the user the ability to save, run, cancel for any type of download, nothing happens. An error does not occur. it does not step over to any part of the code that I know of. It seems to run the ExportToHttpResponse line and then do nothing with it.

So, I was hoping that someone could give me some direction in what I might do wrong with the code found below:

protected void ExportRptButton_Click( object sender, EventArgs e ) { if ( null != SelectedReport ) { rptParams.Clear(); rptParams = null; // Get the report document // string filePath = Server.MapPath( @"~\Reports\" + SelectedReport.FileName + ".rpt" ); // Declare a new Crystal Report Document object and load the report file into the report document. ReportDocument rptDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); ConfigureCrystalReports(rptDoc); // repDoc.Load(rptFileName); // AddParameters(); // Set the report parameters the report object. LoadParameterFields(rptDoc); // Set the static text fields in the report object. LoadStaticTextFields(rptDoc); try { if (rptDoc.IsLoaded) { // Stop buffering the response Response.Buffer = false; // Clear the response content and headers Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; // Export the Report to Response stream in PDF format and file name Customers rptDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "DirectAccessReport"); // rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, "~/PDF_Folder"); // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports } } catch ( Exception ex ) { logger.ErrorFormat("Could not export to pdf! {0}", ex); } } } 

Some notes : the LoadParametersFields / LoadStaticTextFields methods shown above seem to be correct, and when they are used to open a report in crviewer, the report appears and works. Although, if you want to see these methods, I will drop them upon request.

RptParams is initially a declared user of List<ReportParameter>()

The ConfigureCrystalReports method is used to get and load the path to the report file.

Any help or suggestions are welcome. Thanks.

+6
source share
1 answer

This sample code works for me; look at error management.

 protected void btnExport_Click(object sender, EventArgs e) { // Stop buffering the response Response.Buffer = false; // Clear the response content and headers Response.ClearContent(); Response.ClearHeaders(); ExportFormatType format = ExportFormatType.PortableDocFormat; string ext = ".pdf"; string reportName= "myreport"; try { reportDocument.ExportToHttpResponse(format, Response, true, reportName); } catch (System.Threading.ThreadAbortException) { //ThreadException can happen for internale Response implementation } catch (Exception ex) { //other exeptions will be managed throw; } } 
+1
source

Source: https://habr.com/ru/post/920982/


All Articles