cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieving the subreport record count

dave_smith2
Participant
0 Kudos

Hi,

We are using the SAP Crystal runtime for Visual Studio, SP 14 and we are attempting to retrieve the count of records in a subreport, but are unable to do so because the Rows.Count always returns a value greater than 0 even when no data are returned by the subreport.

Sample code:

Private Function SubreportHasData(ByVal reportDocument As Object) As Boolean

Dim sections As Object = reportDocument.ReportDefinition.Sections

For Each section As Object In sections

    Dim reportObjects As Object = section.ReportObjects

    For Each subReportObject As Object In reportObjects

If subReportObject.Kind = 5 Then

Dim subReport As Object = subReportObject.OpenSubreport(subReportObject.SubreportName)

If subReport.Rows.Count > 0 Then Return True

End If

    Next

Next

Return False

End Function


When checking many of the subreport object properties in the IDE, we noticed most of the message values either contained an exception message or that they are not supported in subreports.


Is there a more appropriate property to use?


Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

HI Dave,

Sorry it's in C# but this works:

try

{

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

    {

        switch (rptObj1.Kind)

        {

            // look for sub report object and display info

            case CrReportObjectKindEnum.crReportObjectKindSubreport:

                CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject subObj1;

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

                ++flcnt; // number of subreports

                                  

                SubreportController subreportController = rptClientDoc.SubreportController;

                SubreportClientDocument subreportClientDocument = subreportController.GetSubreport(subObj1.SubreportName);

                int SecNameL = SecName.Length;

                textBox1 = "Imported: " + subreportClientDocument.SubreportLocation.ToString() + "\n";

                int c = (char)(((subObj1.SectionCode) % 1000) + 97); // ;

                textBox1 += SecName.ToString() + " " + c + " : " + rptObj1.Name.ToString() + " = SubObjt: " + subObj1.SubreportName.ToString() + "\n";

                btnReportObjects.Text += textBox1;

                btnReportObjects.Text += "Section Name AA: " + rptObj1.SectionCode.ToString() + " " + rptObj1.SectionName.ToString() + "\n";

                btnReportObjects.AppendText("ToolTip: " + flcnt.ToString() + rptObj1.Format.ToolTipText.ToString() + "\n\n");

                textBox1 = "";

                btnCount.Text = flcnt.ToString();

                // get the SQL from the subreport, may require to be logged on first Not require with saved data report

                GroupPath gp = new GroupPath();

                string tmp = String.Empty;

                btnReportObjects.Text += "Subreport SQL: ";

                try

                {

                    subreportClientDocument.RowsetController.GetSQLStatement(gp, out tmp);

                    btnReportObjects.Text += tmp;

                }

                catch (Exception ex)

                {

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

                    return;

                }

                if (rpt.HasSavedData == true)

                {

                    btnHasSavedData.Checked = true;

                    // get the record count

                    RowsetController boRowsetController;

                    RowsetCursor boRowsetCursor;

                    GroupPath gp1 = new GroupPath();

                    int rows;

                    boRowsetController = subreportClientDocument.RowsetController;

                    boRowsetCursor = boRowsetController.CreateCursor(gp1, new RowsetMetaData(), 0);

                    (boRowsetCursor).RowsetController.RowsetBatchSize = 1000;

                    (boRowsetCursor).Rowset.BatchSize = 1000;

                    (boRowsetCursor).MoveLast();

                    rows = boRowsetCursor.RecordCount;

                    txtRecordCount.Text = rows.ToString();

                }

                else

                    btnHasSavedData.Checked = false;

                break;

        }

    }

}

catch (Exception ex)

{

    btnReportObjects.AppendText("Error in " + SecName + " : " + ex.Message + "\n");

    ++Errorcnt;

    btnErrorCount.Text = Errorcnt.ToString();

}

Thanks again

Don

PS - this works for the  main report object also, just change the report object.

dave_smith2
Participant
0 Kudos

Hi Don,

Thanks for the sample code. We tried it in VB.NET but unfortunately the code did not compile.

Would you be able to provide the full namespace for the GroupPath object and the assembly/dll to which we need to add a reference in our project?

Also is there a simpler method to retrieve the record count for a subreport rather than executing the SQL for the subreport as it appears you are doing? The main report had a property called Rows.Count which returned the number of records, but it does not appear to work for a subreport. Is there nothing similar to that for subreports?

Our objective is to return the record count at runtime. There would not be any saved data.

Thanks.

0 Kudos

Ah yes... you need all of these, or most of them as well as assigning the rpt object to the ReportClientDocument:

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using CrystalDecisions.ReportAppServer;

using CrystalDecisions.ReportAppServer.ClientDoc;

using CrystalDecisions.ReportAppServer.Controllers;

using CrystalDecisions.ReportAppServer.ReportDefModel;

using CrystalDecisions.ReportAppServer.CommonControls;

using CrystalDecisions.ReportAppServer.CommLayer;

using CrystalDecisions.ReportAppServer.CommonObjectModel;

using CrystalDecisions.ReportAppServer.ObjectFactory;

using CrystalDecisions.ReportAppServer.Prompting;

using CrystalDecisions.ReportAppServer.DataSetConversion;

using CrystalDecisions.ReportAppServer.DataDefModel;

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

CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;

rpt.Load(rptName.ToString(), OpenReportMethod.OpenReportByTempCopy);

rptClientDoc = rpt.ReportClientDocument;

Should get it to build.

Don

Answers (0)