cancel
Showing results for 
Search instead for 
Did you mean: 

How to modify sub-report location with C#

Former Member
0 Kudos

Hi,

Is it possible to modify sub-report location with C#?

I use the following code to retrieve the locations of sub-reports. What if I need to update the SubreportLocation and save it back to the main template report?


We need this for our production deployment. Let's say we have two sets of Crystal reports, Dev and Prod. All Dev templates are stored in C:\Dev and all sub-reports are in C:\Dev\SubReport. All Prod templates and sub-reports are stored in C:\Prod and C:\Prod\SubReport. If we need to update a template, we will do it in Dev and test it, then deploy to Prod. What is the best way to do the deployment? We don't want to update all sub-report locations every time. I don't think sub-report location suppports relative path right?

               ISCDReportClientDocument rptClientDoc = cryRpt.ReportClientDocument;

            var rptObjs = cryRpt.ReportClientDocument.ReportDefController.ReportObjectController.GetAllReportObjects();

            var textBox1 = "";

            try

            {

                foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject rptObj1 in rptObjs)

                {

                    // look for sub report object and display info

                    if (rptObj1.Kind == CrReportObjectKindEnum.crReportObjectKindSubreport)

                    {

                       

                        CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject subObj1;

                        subObj1 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject) rptObj1;

                        SubreportController subreportController = rptClientDoc.SubreportController;

                        var location =

                            ((CrystalDecisions.ReportAppServer.ReportDefModel.ISCRSubreportObject)(subObj1))

                                .SubreportLocation;

                        SubreportClientDocument subreportClientDocument =

                            subreportController.GetSubreport(subObj1.SubreportName);

                        textBox1 = textBox1 + "Imported: " + subObj1.SubreportLocation + "\n";

                    }

                }

            }

            catch (Exception ex)

            {

               

            }

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Pradeep,

Use the Object browser when using these API's to see what it is capable of. In this case the SubreportLocation has a Set/Get option so you can update it:

string SubreportLocation { set; get; }

    Member of CrystalDecisions.ReportAppServer.Controllers.ISCRSubreportClientDocument

You should be able to simply set the new location, if that does not work though then Clone the subreport Object first and then update the location.

Don

Former Member
0 Kudos

Hi Don,

Thanks fro the quick response. I already tried that, but when it didn't affect the main template when I saved it.

Just wonder what the proper deployment process should be for template/sub-reports?

CrystalDecisions.CrystalReports.Engine.ReportDocument cryRpt = new                CrystalDecisions.CrystalReports.Engine.ReportDocument();

            Console.WriteLine(File.Exists(tempaltePath) ? "File exists." : "File does not exist.");

            if (!File.Exists(tempaltePath)) throw new Exception(tempaltePath + " does not exist.");

            cryRpt.Load(tempaltePath);

            var lastModifiedTime = Convert.ToDateTime(GetSpecificFileProperties(tempaltePath, 3));

            var lastModifiedUser = GetSpecificFileProperties(tempaltePath, 10);

            Logon(cryRpt, "", "ReportExtract");

            cryRpt.SetParameterValue("DATASET_ID", DatasetId);

           

            ISCDReportClientDocument rptClientDoc = cryRpt.ReportClientDocument;

            var rptObjs = cryRpt.ReportClientDocument.ReportDefController.ReportObjectController.GetAllReportObjects();

            var textBox1 = "";

            try

            {

                foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject rptObj1 in rptObjs)

                {

                    // look for sub report object and display info

                    if (rptObj1.Kind == CrReportObjectKindEnum.crReportObjectKindSubreport)

                    {

                       

                        CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject subObj1;

                        subObj1 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject) rptObj1;

                        SubreportController subreportController = rptClientDoc.SubreportController;

                        var location =

                            ((CrystalDecisions.ReportAppServer.ReportDefModel.ISCRSubreportObject)(subObj1))

                                .SubreportLocation;

                        ((CrystalDecisions.ReportAppServer.ReportDefModel.ISCRSubreportObject) (subObj1))

                            .SubreportLocation = "c:/123.rpt";

                        SubreportClientDocument subreportClientDocument =

                            subreportController.GetSubreport(subObj1.SubreportName);

                        textBox1 = textBox1 + "Imported: " + subObj1.SubreportLocation + "\n";

                    }

                }

            }

            catch (Exception ex)

            {

               

            }

            cryRpt.SaveAs(@"C:\test.rpt");

0 Kudos

Try previewing and/or exporting to RPT format. SaveAs may not retain the changes you did. YOu may also need to do a VerifyDatabase(); so the engine can can validate all of the changes you did.

Don

Former Member
0 Kudos

Hi Don, I tried to export to RPT, but somehow the output file cannot be opened. I modified the existing code for PDF export. It works for PDF prefecture. I didn't make any change on the report. I just tried to load and export the same file.

CrystalDecisions.CrystalReports.Engine.ReportDocument cryRpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

cryRpt.Load(tempaltePath);

cryRpt.SetParameterValue("DATASET_ID", DatasetId);

try

            {

                var crDiskFileDestinationOptions = new DiskFileDestinationOptions { DiskFileName = @"C:\test.rpt" }; // It was .pdf

                var crExportOptions = cryRpt.ExportOptions;

                {

                    crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;

                    crExportOptions.ExportFormatType = ExportFormatType.CrystalReport; // it was ExportFormatType.PortableDocFormat

                    crExportOptions.DestinationOptions = crDiskFileDestinationOptions;

                    crExportOptions.FormatOptions = new ExportOptions(); // it was PdfRtfWordFormatOptions()

                }

                cryRpt.Export();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

0 Kudos

See if this works for you:

// This gets populated when you click on the export ENUM

string ExportTypeSelected = lstExportFormatType.SelectedItem.ToString();

if (ExportTypeSelected == "crReportExportFormatCrystalReports")

#region RPT

{

    // This works do not alter

    // this gets the report name and sets the export name to be the same less the extension

    string outputFileName = "";

    string MyRptName = rpt.FileName.ToString();

    outputFileName = MyRptName.Substring(9, rpt.FileName.Length - 9);

    outputFileName = outputFileName.Substring(0, (outputFileName.Length - 4)) + "1.rpt";

    try

    {

        if (File.Exists(outputFileName))

        {

            File.Delete(outputFileName);

        }

        CrystalDecisions.ReportAppServer.ReportDefModel.RPTExportFormatOptions RasRPTExpOpts = new RPTExportFormatOptions();

        try

        {

            RasRPTExpOpts = rptClientDoc.get_SavedExportOptions(CrReportExportFormatEnum.crReportExportFormatCrystalReports);

        }

        catch (Exception ex)

        {

            btnSQLStatement.Text = "ERROR: " + ex.Message;

            //return;

        }

        // Set them now:

        //RasPDFExpOpts.CreateBookmarksFromGroupTree = false;

        //RasPDFExpOpts.EndPageNumber = 1;

        //RasPDFExpOpts.StartPageNumber = 1;

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

        exportOpts1.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatCrystalReports;

        exportOpts1.FormatOptions = RasRPTExpOpts;

        // And Export

        rptClientDoc.PrintOutputController.ExportEx(exportOpts1).Save(outputFileName, true);

        MessageBox.Show("Export to RPT Completed. NOTE: report is *1.RPT", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    catch (Exception ex)

    {

        btnSQLStatement.Text = "ERROR: " + ex.Message;

        return;

    }

    // This works do not alter

Former Member
0 Kudos

Thanks Don. Unfortunately it didn't work. Same issue. The file got exported but it crashed the application while opening it.

The original code gives an exception

I look into the tool you wrote(the tool is really helpful), still no luck.

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

            RASexportOpts.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatCrystalReports;

            rptClientDoc.PrintOutputController.ExportEx(RASexportOpts).Save(@"C:\test.rpt", true);

            CrystalDecisions.ReportAppServer.ReportDefModel.RPTExportFormatOptions RasRPTExpOpts = new RPTExportFormatOptions();

            try

            {

                RasRPTExpOpts = rptClientDoc.get_SavedExportOptions(CrReportExportFormatEnum.crReportExportFormatCrystalReports);

            }

            catch (Exception ex)

            {

                throw;

                //return;

            }

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

            exportOpts1.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatCrystalReports;

            exportOpts1.FormatOptions = RasRPTExpOpts;

            rptClientDoc.PrintOutputController.ExportEx(exportOpts1).Save(@"C:\test1.rpt", true);

            CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();

            diskOpts.DiskFileName = @"C:\test2.rpt";

            CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();

            exportOpts.DestinationOptions = diskOpts;

            exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;

            exportOpts.ExportFormatType = ExportFormatType.CrystalReport;

            try

            {

                cryRpt.Export(exportOpts);

            }

            catch (Exception ex)

            {

                throw;

                return;

            }

0 Kudos

You are missing something then.

Did you install CR for VS from this link:

First one to integrate into VS.

Do not install just the MSI, those are for redist packages only.

Don

Former Member
0 Kudos

Yes, I did install the .exe

0 Kudos

You are installing both the EXE to integrate into VS and the 32 and 64 bit MSI's. Read the instructions, DON'T install the MSI's on the DEV PC.

Please note: To integrate "SAP Crystal Reports, developer version for Microsoft Visual Studio" you must run the Install Executable. Running the MSI will not fully integrate Crystal Reports into VS. MSI files by definition are for runtime distribution only.

Answers (0)