Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExportToHttpResponse Crystal Report without running CRViewer

So i am attempting to export a crystal report to pdf without opening the report in the runtime viewer of my web application using the

ExportToHttpResponse

method. Everything seems to be performing correctly when it comes to loading the parameters, getting the filename/path loading the report. But when i execute the part that is suppose to create a popup dialog box giving the user the option to save,run,cancel for any type of download nothing happens. No error is thrown. it isn't stepping over any part of the code that i'm aware of. It seems to run the ExportToHttpResponse line and then do nothing with it.

So i was hoping someone could give me some direction in what i could be doing 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 displayed above seem to be working correctly and when used to open the report in the crviewer the report comes up and works. Although, if you wish to see those methods as well i'll throw them up on request.

The rptParams in the beginning is a privately declared List<ReportParameter>()

the ConfigureCrystalReports method is used to obtain and load the filepath of the report.

any help or suggestions are greatly appreciated. Thank you.

like image 274
James213 Avatar asked Jul 20 '12 17:07

James213


1 Answers

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;
    }
}
like image 98
Emanuele Greco Avatar answered Sep 21 '22 11:09

Emanuele Greco