cancel
Showing results for 
Search instead for 
Did you mean: 

Exception on exporting a report

12-28-2011 7:13 PM
1894 views 25 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

When I'm attempting to export a report I receive the following exception:

Object reference not set to an instance of an object.

at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export(ExportOptions options)

I even went to the point of copying code from the examples in the help documents but I cannot determine which object is not properly created.

Here is the code I'm having trouble with:

BOOL CST_Crpe::Export( PCHAR pcDest )

{

BOOL bRval = TRUE;

try

{

ExportOptions^ exportOpts = gcnew ExportOptions();

DiskFileDestinationOptions^ diskOpts = ExportOptions::CreateDiskFileDestinationOptions();

exportOpts->ExportFormatType = ExportFormatType::RichText;

exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;

diskOpts->DiskFileName = gcnew String( pcDest );

exportOpts->ExportDestinationOptions = diskOpts;

m_pDoc->Export( exportOpts );

}

catch( Exception^ e )

{

bRval = FALSE;

m_strMessage = e->Message + "\r\n" + e->StackTrace;

}

return( bRval );

}

What am I doing wrong or what do I need to do to make this work?

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

0 Likes

Hello,

This looks like you are using C++, Managed I hope?

See these samples to get you going:

[Crystal Reports .NET C++ - Get Viewer Events|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/c061bb21-efbd-2b10-899b-c6c0c18881b8]

This C++ .NET sample Windows application demonstrates how to access the Viewer Events for the business objects windows forms viewer.

[Crystal Reports .NET C++ - View Report|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/50b75734-f1bd-2b10-c7b5-a6c020e5ee07]

This sample application shows you how to preview a Crystal Report in a .NET C++ application.

I don't have any sample C++ but In C# here's some to get you going:


private void ExportToStream_Click(object sender, EventArgs e)
{
    rptClientDoc = rpt.ReportClientDocument;

    CrystalDecisions.Shared.MicrosoftMailDestinationOptions mailOpts = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions();
    CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
    CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
    CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions csvExpOpts = new CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions();
    CrystalDecisions.Shared.HTMLFormatOptions HTMLExpOpts = new CrystalDecisions.Shared.HTMLFormatOptions();
    CrystalDecisions.Shared.PdfFormatOptions PDFExpOpts = new CrystalDecisions.Shared.PdfFormatOptions();
    CrystalDecisions.Shared.EditableRTFExportFormatOptions RTFExpOpts = new CrystalDecisions.Shared.EditableRTFExportFormatOptions();
    CrystalDecisions.Shared.PdfRtfWordFormatOptions PDFRTFExpOpts = new CrystalDecisions.Shared.PdfRtfWordFormatOptions();
    CrystalDecisions.Shared.ExcelDataOnlyFormatOptions XLSXExpOpts = new CrystalDecisions.Shared.ExcelDataOnlyFormatOptions();

    //rpt.Load(@"c:\reports\b.rpt");

    //rptClientDoc = new ReportClientDocument();

    //diskOpts.DiskFileName = "c:\\ReportName.csv";
    //diskOpts.DiskFileName = @"c:\tmp\html\b.html";
    diskOpts.DiskFileName = @"c:\reports\worldsales.pdf";
    //diskOpts.DiskFileName = @"c:\reports\worldsales.rtf";
    //diskOpts.DiskFileName = @"c:\reports\inventory.xlsx";

Edited by: Don Williams on Dec 28, 2011 6:04 PM

0 Likes

Continued:

exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;

//exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues;

//exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML32;

exportOpts.ExportDestinationOptions = diskOpts;

CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions exportOptions = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();

//This sets which format we will export to.

exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;

//Create the FormatOptions class for PDF.

PDFExportFormatOptions PDFexportOptions = new PDFExportFormatOptions();

PDFexportOptions.StartPageNumber = 1;

PDFexportOptions.EndPageNumber = 1;

PDFexportOptions.CreateBookmarksFromGroupTree = true;

//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;

////Create the FormatOptions class for Editable RTF.

//RPTExportFormatOptions RTFExpOptions = new RPTExportFormatOptions();

////Set the export format options with the page range we just set.

//exportOptions.FormatOptions = RTFExpOptions;

////This sets which format we will export to.

//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;

////RTFExpOptions.StartPageNumber = 1;

////RTFExpOptions.EndPageNumber = 2;

////RTFExpOptions.DiscardUnselectedRecords = false;

//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatXLSX;

//This exports the report to a byte() that we will stream out.

Byte[] oByte = (Byte[])rptClientDoc.PrintOutputController.ExportEx(exportOptions).ByteArray;

//////csvExpOpts = new CharacterSeparatedValuesFormatOptions();

//////csvExpOpts.GroupSectionsOption = CsvExportSectionsOption.Export;

//////csvExpOpts.ReportSectionsOption = CsvExportSectionsOption.Export;

//////csvExpOpts.SeparatorText = ",";

//////csvExpOpts.Delimiter = "|";

//////HTMLExpOpts = new HTMLFormatOptions();

////////HTMLExpOpts.HTMLFileName = @"c:\tmp\html\phonebook report that looks real purdy.html";

//////HTMLExpOpts.HTMLBaseFolderName = @"c:\tmp\html\b.html";

//////HTMLExpOpts.HTMLEnableSeparatedPages = false;

//////HTMLExpOpts.UsePageRange = false;

//////HTMLExpOpts.HTMLHasPageNavigator = false;

//System.IO.Stream oStream;

//byte[] byteArray = null;

//oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues);

//oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

//byteArray = new byte[oStream.Length];

//oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));

//oStream.Read(oByte, 0, Convert.ToInt32(oStream.Length - 1));

// this is used to verify the file so I saved it to disk

System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oByte.Length)).Close();

System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length));

System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);

//oStream.Close();

GC.Collect();

MessageBox.Show("Export to Stream complete", "RAS", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Don

Former Member
0 Likes

Yes I am using C++/CLI. I've even tried copying and pasting samples from the SDK documents and only changing the name of the export file to match my system.

Nothing I've tried seems to work and I have no idea which object is not valid. If I knew that, I'd have a better chance of fixing any issues.

FWIW, the report seems to export fine from the designer.

Former Member
0 Likes

It seems to me that the problem might be related to the ExportRequestContext sent into the format engine. I don't see a way to evaluate this or I've not seen anything in samples that show anything about setting the report object up so that it can create the context correctly.

Former Member
0 Likes

ExportToStream has the same exception.

The report is a local report, not server based.

More and more, I'm thinking that there is something I'm not doing that creates the ExportRequestContext in the lower layers. From the ReportDocument object, I do not have access to the FormatEngine to manage it or interrogate it to see what is wrong there.

0 Likes

Try this, it does look like you are missing some properties: ( sorry C# again )


            CrystalDecisions.Shared.EditableRTFExportFormatOptions RTFExpOpts = new CrystalDecisions.Shared.EditableRTFExportFormatOptions();

            rpt.Load(@"c:\reports\Group.rpt");

            // this doesn't do anything, just wanted to see what options there are. Set to false so it doesn't use the page range
            RTFExpOpts.FirstPageNumber = 1;
            RTFExpOpts.LastPageNumber = 1;
            RTFExpOpts.UsePageRange = false;
            diskOpts.DiskFileName = "c:\\reports\\Group.rtf";

            exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            exportOpts.ExportFormatOptions = RTFExpOpts;
            exportOpts.ExportDestinationOptions = diskOpts;

            exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.EditableRTF;

            try
            {
                rpt.Export(exportOpts);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message);
            }  

Don

Former Member
0 Likes

Still no good.

I've tried using the PdfFormatOptions and EditableRTFExportFormatOptions classes. I filled out what I could in each and still the same behavior in the same location with the same exception stack trace.

0 Likes

Hello,

You did not look close enough, you are missing one import part...

EditableRTFExportFormatOptions^ RTFExpOpts = gcnew EditableRTFExportFormatOptions();

So I did this and it works for me:


 {
	boReportDocument = gcnew ReportDocument();
	boReportDocument->Load("C:\\Reports\\World Sales Report.rpt");

	ExportOptions^ exportOpts = gcnew ExportOptions();
	DiskFileDestinationOptions^ diskOpts = ExportOptions::CreateDiskFileDestinationOptions();

	EditableRTFExportFormatOptions^ RTFExpOpts = gcnew EditableRTFExportFormatOptions();

	diskOpts->DiskFileName = gcnew String( "c:\\reports\\World.rtf" );

	exportOpts->CreateEditableRTFExportFormatOptions();

	exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;
	exportOpts->ExportFormatOptions = RTFExpOpts;
	exportOpts->ExportDestinationOptions = diskOpts;

	exportOpts->ExportFormatType = ExportFormatType::RichText;
	boReportDocument->Export( exportOpts );

 }

Don

Former Member
0 Likes

I've been over that ground several times. It doesn't work for me.

Former Member
0 Likes

Now I'm starting to wonder if there is a package missing or something didn't get registered properly during the install.

0 Likes

did you add:

"C:\Program Files (x86)\Business Objects\Common\4.0\managed"

to Project Properties in C++, General, Additional Include Directories

and "C:\Program Files (x86)\Business Objects\Common\4.0\managed\dotnet2"

To Resolve #using references?

Former Member
0 Likes

Yes I did add those paths and namespaces or the whole thing won't compile.

0 Likes

mmm....

All i did was take that sample app - Crystal Reports .NET C++ - View Report

Updated the reference paths and Added a button to the viewer form and then added your code and made a few changes as follows:


private: System::Void btnExport_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			boReportDocument = gcnew ReportDocument();
			boReportDocument->Load("C:\\Reports\\World Sales Report.rpt");

			ExportOptions^ exportOpts = gcnew ExportOptions();
			DiskFileDestinationOptions^ diskOpts = ExportOptions::CreateDiskFileDestinationOptions();

			EditableRTFExportFormatOptions^ RTFExpOpts = gcnew EditableRTFExportFormatOptions();

			diskOpts->DiskFileName = gcnew String( "c:\\reports\\World.rtf" );

			exportOpts->CreateEditableRTFExportFormatOptions();

			exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;
			exportOpts->ExportFormatOptions = RTFExpOpts;
			exportOpts->ExportDestinationOptions = diskOpts;

			exportOpts->ExportFormatType = ExportFormatType::RichText;
			boReportDocument->Export( exportOpts );

		 }

Are you using a report with saved data? If not you'll have to add logon info also. I'm using the World Sales Report from our samples:

https://smpdl.sap-ag.de/~sapidp/012002523100005852352008E/cr_xi_xtreme_rep_smpl_en.zip

Any other version of CR runtime on your PC?

Use Modules to verify you are using all Version 12.x.x.x assemblies.

Don

Former Member
0 Likes

I'm not using a report with saved data.

I am also not using ODBC. I am using a file system based connection where there is no connection string or log in.

Is there a chance that since I'm making all the calls from a CLR DLL and a Win32 console application and not using a WinForm or any GUI causes this? Does the API require that it be running in a WinForm or some other GUI based app? That could be a deal breaker too because this has to be able to be run from a background service.

0 Likes

OK then start over... simplify as much as possible....

No it does not require a form nor a gui but to test it makes things much easier, what works in a form should also work in a console app.

Start off with a report with saved data, this removes all data source problems. Take that sample app and simply remove the Viewer and comment out the report load in the form.

Click the export button, does that produce an output? I assume it does not.

So now go back to the set data source routine, which is likely the cause of the exception.

Also, CR is based on VS 2005 C++ Runtime libraries, depending on your OS you may need to install it as well as having your Framework 2.0 -> 3.5 updated.

You may want to start using C#, it's basically the same as C++ and manages CR runtime easier than C++. And Console app's are just fine in C# also.

Don

Former Member
0 Likes

The VS 2005 libraries are installed so I won't blame them.

I have to use C++ at some point because the legacy code is writen in C and I can't go re writing our entire software. Especially since almost all of it is native code interacting with hardware through native driver calls. Re writing it all in C# and wrapping the lower levels with CLI code would take months if not a year or more.

This is one of the things I'm not fond of with this product. Moving to 100% .NET doesn't make sense when I have to write CLI code so I can interface native code to .NET so that .NET can then translate back down to native. I'd prefer to simply go from native to native and avoid all the translation layers.

I doubt loading a report with stored data will change much but I'll try. The big problem, is that this is not an option in the live environtment. The environment this is to run in is very dynamic, with real time data that changes and adds several times a second. I can't have a service running in the background load a designer, create a repor with the data stored in it and then print it out using this API.

0 Likes

It was only a suggestion not knowing how big your app is then I agree, no point in spending the DEV time to rewrite....

What I am suggesting is to SIMPLIFY the tests. Anything could be the problem and until the Export is called you won't know the cause. Exporting is the last routine so it's the one that throws the error. Using a report with saved data takes the DB part out of the picture, and again it's only a test.

Use the test app, its simple, then once exporting works with saved data then add your Record Set or XML as a data source.

Or try DebugDiag, it may show you the specific dll or API it's erroring in, maybe...

Don

Former Member
0 Likes

The code seems very simple to me and rather minimalist.

Maybe I'll try printing to a printer and see if that still works.

Former Member
0 Likes

Is this simple enough?

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Security;
using namespace System::Collections;
using namespace System::Drawing::Printing;
using namespace System::Runtime::InteropServices;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace CrystalDecisions::CrystalReports::Engine;
using namespace CrystalDecisions::Shared;

int main(array<System::String ^> ^args)
{
    try
	{
		ReportDocument^ doc = gcnew ReportDocument();
		ExportOptions^ exp = gcnew ExportOptions();
		DiskFileDestinationOptions^ opts = ExportOptions::CreateDiskFileDestinationOptions();
		opts->DiskFileName = "D:\\dev8.0\\Report\\CallDetail.pdf";
		exp->ExportDestinationOptions = opts;
		exp->ExportDestinationType = ExportDestinationType::DiskFile;
		exp->ExportFormatOptions = exp->CreatePdfFormatOptions();
		exp->ExportFormatType = ExportFormatType::PortableDocFormat;

		doc->FileName = "D:\\dev8.0\\MyData\\CallDetail.rpt";
		doc->Export( exp );
	}
	catch( Exception^ e )
	{
		Console::WriteLine( e->Message + "\r\n" + e->StackTrace );
	}

    return 0;
}

output:

Object reference not set to an instance of an object.

at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export(ExportOptions options)

at main(String[] args) in d:\testing\st_crpe\cpptest\cpptest.cpp:line 30

0 Likes

Hello,

You are still not using my code:


boReportDocument = gcnew ReportDocument();
boReportDocument->Load("C:\\Reports\\World Sales Report.rpt");

ExportOptions^ exportOpts = gcnew ExportOptions();
DiskFileDestinationOptions^ diskOpts = ExportOptions::CreateDiskFileDestinationOptions();

//EditableRTFExportFormatOptions^ RTFExpOpts = gcnew EditableRTFExportFormatOptions();
CrystalDecisions::Shared::PdfFormatOptions^ PDFExpOpts = gcnew CrystalDecisions::Shared::PdfFormatOptions();

//diskOpts->DiskFileName = gcnew String( "c:\\reports\\World.rtf" );
diskOpts->DiskFileName = gcnew String( "c:\\reports\\World.pdf" );

exportOpts->CreateEditableRTFExportFormatOptions();

exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;
//exportOpts->ExportFormatOptions = RTFExpOpts;
exportOpts->ExportFormatOptions = PDFExpOpts;
exportOpts->ExportDestinationOptions = diskOpts;

exportOpts->ExportFormatType = ExportFormatType::PortableDocFormat;
boReportDocument->Export( exportOpts );

Change this one line to load your report with saved data and see what happens?


boReportDocument->Load("D:\\dev8.0\\MyData\\CallDetail.rpt");

I believe what the problem is is you are setting the options and then loading a report. Load the report first then the default properties are not cleared.

Edited by: Don Williams on Dec 30, 2011 9:32 AM

Former Member
0 Likes

OK so here's the complete code I used. I only changed the name of the report and the destination to reflect what I have.


// CPPTest.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Security;
using namespace System::Collections;
using namespace System::Drawing::Printing;
using namespace System::Runtime::InteropServices;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace CrystalDecisions::CrystalReports::Engine;
using namespace CrystalDecisions::Shared;

int main(array<System::String ^> ^args)
{
    try
	{
		ReportDocument^ boReportDocument = gcnew ReportDocument();
		boReportDocument->Load("D:\\dev8.0\\MyData\\\CallDetail.rpt");
 
		ExportOptions^ exportOpts = gcnew ExportOptions();
		DiskFileDestinationOptions^ diskOpts = ExportOptions::CreateDiskFileDestinationOptions();
 
		//EditableRTFExportFormatOptions^ RTFExpOpts = gcnew EditableRTFExportFormatOptions();
		CrystalDecisions::Shared::PdfFormatOptions^ PDFExpOpts = gcnew CrystalDecisions::Shared::PdfFormatOptions();
 
		//diskOpts->DiskFileName = gcnew String( "c:\\reports\\World.rtf" );
		diskOpts->DiskFileName = gcnew String( "D:\\dev8.0\\Report\\CallDetail.pdf" );
 
		exportOpts->CreateEditableRTFExportFormatOptions();
 
		exportOpts->ExportDestinationType = ExportDestinationType::DiskFile;
		//exportOpts->ExportFormatOptions = RTFExpOpts;
		exportOpts->ExportFormatOptions = PDFExpOpts;
		exportOpts->ExportDestinationOptions = diskOpts;
 
		exportOpts->ExportFormatType = ExportFormatType::PortableDocFormat;
		boReportDocument->Export( exportOpts );
	}
	catch( Exception^ e )
	{
		Console::WriteLine( e->Message + "\r\n" + e->StackTrace );
	}

    return 0;
}

Output:

Object reference not set to an instance of an object.

at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)

at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export(ExportOptions options)

at main(String[] args) in d:\testing\st_crpe\cpptest\cpptest.cpp:line 40

There has to be something else wrong.

Answers (1)

Answers (1)

former_member188030
Active Contributor
0 Likes

Hi,

More information required

- Version of Crystal Report

- Version of Visual Studio

- Win or web app

- Development or production machine?

If the report has any parameters, set the parameter values first and then export the report.

If not, just as a test, view the report and try exporting it from the CR viewers export button, see how it behaves.

- Bhushan.

Former Member
0 Likes

- Version of Crystal Report

2008

- Version of Visual Studio

2008

- Win or web app

Win

- Development or production machine?

Currently development.

I first previewed the report in the designer, converted it to the 2008 version of CR. Saved the report. I've tried other settings and the API does not error when I do but there is no change in the experience. I've tried the ReportDocument::Export and ReportDocument::ExportToFile. Both experience the exception.

My problem is that the exception is so generic, that you have no idea which reference is invalid.

former_member188030
Active Contributor
0 Likes

Does the report export fine from the CR 2008 designer?

Have you tried using ExportToStream?

Former Member
0 Likes

No but I don't think that would make much difference as Export and ExportToFile both wrap ExportToStream.