0
votes

I have a single Crystal Report that I am trying to run via a web site using a Report Viewer and also (using the same .rpt) via a background process that would generate the report straight to a stream or file using the export options.

I am not using a database, but have created an xml schema file and am generating an xml data file to load into the report based on unique user data.

When I created the report I made an ADO.NET(XML) connection to design it and pointed it to my schema file (all within VS 2012).

At run time I use a .net DataSet object and use the ReadXmlSchema and ReadXml methods to get my report data and then just set the datasource of my ReportDocument object.

This all worked great in my web application using Report Viewer. Here is the working code:

    ReportDocument report = new ReportDocument();
    report.Load(reportPath);

    DataSet reportData = new DataSet();

    reportData.ReadXmlSchema("MySchema.xml");
    reportData.ReadXml("SampleData1.xml");

    report.SetDataSource(reportData);

    CrystalReportViewer1.ReportSource = report;

My issue/question is how to run this programmatically with no Report Viewer and have it generate a PDF? Essentially the same code as above minus the Report Viewer piece and adding one of the Export options. I have a long running background process and would like to attach this report as a PDF to an email I generate.

I am able to use the same methodology as the web version, but when I get to the SetDataSource line and try to set it to my DataSet I receive and error saying 'Unknown Database Connection Error...' having to do with not using the SetDatabaseLogon method.

I am trying to understand what Crystal Reports expects in the way of the SetDatabaseLogon method when I am not connecting to a database. If it assumes my original ADO.NET(XML) connection in the .RPT file is a legitimate database connection, then what are the logon parameters? It's just a file. There is no username, password etc.

Is there a better way to do a straight to PDF from an existing Crystal Report without Report Viewer? I have looked at various links but found nothing that does not involve some kind of connection string. If I must use SetDatabaseLogin, what values will work when using XML files only?

Thanks for any information or links!

Edit: In looking at the "Unknown Database Connector Error" (not Connection as I stated earlier) I see that it is actually looking at the .RPT in my Temp folder and not in my Remote location where I thought I was loading it from. Maybe this is the problem?

3
Okay so I added 'useLegacyV2RuntimeActivationPolicy="true"' to the startup element in the app.config folder and now it runs fine. Have no idea why. I got the answer from stackoverflow.com/questions/5596501/…user2564788

3 Answers

1
votes

Have you tried something like this:

Report.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("Foldername/Reportname.pdf"));
0
votes

Try this code:

                report.ExportOptions.DestinationType = CRExportDestinationType.crEDTDiskFile;
                report.ExportOptions.FormatType = CRExportFormatType.crEFTPortableDocFormat;
                DiskFileDestinationOptions fileOption = new DiskFileDestinationOptions();
                fileOption.DiskFileName = "Test.pdf";
                report.ExportOptions.DiskFileName = fileOption.DiskFileName;
                report.Export(false);
0
votes

You could try this:

PageMargins margins = new PageMargins { topMargin = 100, leftMargin = 250, bottomMargin = 100, rightMargin = 100 };
report.PrintOptions.ApplyPageMargins(margins );
Stream str = report.ExportToStream(ExportFormatType.PortableDocFormat);
int length = Convert.ToInt32(str.Length);
byte[] bytes = new byte[length];
str.Read(bytes, 0, length);
str.Close();
File.WriteAllBytes(@"C:\Report.pdf", bytes);

I hope it helps.