0
votes

I'm new to Crystal Reports and promptly got a problem while playing with CR for Visual Studio.

I designed a Report that consists of two Subreports only. The main report has no further data at all. I created a DataSet (xsd) with two tables - one for each subreport. In Visual Studio I created both subreports and added the correct table to insert the data fields. I didn't add a DataTable to the main report because it has no own data. This was done in Visual Studio using the template designer (not sure if correct name)

In code I add some dummy data to both tables of the DataSet and add the correct table to the corresponding subreport. Then the report is shown in the Crystal Report Viewer. But there is no data at all, I get an empty page.

I already confirmed that the DataTables are correctly filled with my dummy data and that the correct report and subreports are used.

What am I doing wrong? See my code below:

 DataSet1 ds1 = new DataSet1(); // my DataSet with 2 tables
            report.DataSourceConnections.Clear(); //report is the ReportDocument with the correct Report

            DataTable t1 = ds1.Tables.Add("DataTable1");
            t1.Columns.Add("Id", Type.GetType("System.String"));
            t1.Columns.Add("Feld1", Type.GetType("System.String"));

            DataTable t2 = ds1.Tables.Add("DataTable2");
            t2.Columns.Add("Id", Type.GetType("System.String"));
            t2.Columns.Add("Feld1", Type.GetType("System.String"));
            t2.Columns.Add("Feld2", Type.GetType("System.String"));

            DataRow r;
            int i = 0;
            for (i = 0; i <= 9; i++)
            {
                r = t1.NewRow();
                r["Id"] = i.ToString();
                r["Feld1"] = string.Format("Item1{0}", i);
                t1.Rows.Add(r);
            }

            DataRow r2;
            int i2 = 0;
            for (i2 = 0; i2 <= 9; i2++)
            {
                r2 = t2.NewRow();
                r2["Id"] = i2.ToString();
                r2["Feld1"] = string.Format("Item1{0}", i2);
                r2["Feld2"] = string.Format("Item2{0}", i2);
                t2.Rows.Add(r2);
            }

            report.Subreports[0].DataSourceConnections.Clear();
            report.Subreports[1].DataSourceConnections.Clear();
            report.Subreports[0].SetDataSource(ds1.Tables[0]);
            report.Subreports[1].SetDataSource(ds1.Tables[1]);

The report is exported into a PDF file:

report.ExportToDisk(ExportFormatType.PortableDocFormat, "D:\\test.pdf");
1
In wich section did you put your subreports? If you put them in details section, they will not be shown, because details section do not show up if the main report has no data.heringer
So the main report has to have own data for subreports to be shown? Why? And what do I do, if the main report just acts as a container for different subreports?Newbie
After much trial and error I noticed that the main report only needs a DataTable added via the Field Explorer. I don't even need to add some fields of that DataTable into the main report for the supreports to show. Is this considered a bug or a feature?Newbie
The main report does not need data. Only if you want to use the details section. You may try to move your subreports to the report header section.heringer
@heringer: see my comment in your answer below.Newbie

1 Answers

0
votes

So, based on the comments above, i understand the best solution would be move your subreports to the report header.