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");