user6743262
user6743262

Reputation:

Error when trying to set dataset to crystal report source

I have a physical dataset file in which I did this:

enter image description here

In the CR designer I set that dataset to a source like this:

enter image description here

The problem I am facing in the c# code is at this line:

document.SetDataSource(rv);

Where document is a pre loaded CR document and rv is a dataset which looks like this just before the mentioned line:

enter image description here

Finally, when I come to that line I get this error. Not even exception is thrown...

enter image description here

Can anyone help me with this?

This is my code:

private void GenerateReport_Click(object sender, EventArgs e)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument document = null;
                document = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                document.Load(@"C:\output\CrystalReportTest.rpt");

                DataTable dt = new DataTable();
                dt.Columns.Add("FirstName");
                dt.Columns.Add("LastName");
                dt.Columns.Add("State");
                dt.Columns.Add("Town");
                dt.Columns.Add("ID");

                dt.Rows.Add(new object[] { "James", "Bond", "England", "London", "1233428749020" });
                dt.Rows.Add(new object[] { "Rocky", "Balboa", "USA", "Los Angeles", "471998425060" });
                dt.Rows.Add(new object[] { "Keyser", "Soze", "Hungary", "Budapest", "3643428747898" });

                List<DataTable> ldt = new List<DataTable>();
                ldt.Add(dt);


                System.Data.DataSet rv = new System.Data.DataSet();
                rv = AddTableInReportDataSet(ldt);

                if (rv != null)
                {
                    document.SetDataSource(rv);
                }
                document.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"C:\output\Export_Test.pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
        }

        private void SetMainReportDataSource(CrystalDecisions.CrystalReports.Engine.ReportDocument document, List<DataTable> tables)
        {
            if (tables.Count > 0)
            {
                DataSet reportDataSet = AddTableInReportDataSet(tables);
                document.SetDataSource(reportDataSet);
            }
        }

        private DataSet AddTableInReportDataSet(List<DataTable> tableList)
        {
            DataSet rv = new DataSet();
            tableList.ForEach(tbl => AddTableInReportDataSet(rv, tbl));
            return rv;
        }

        private void AddTableInReportDataSet(DataSet dataSet, DataTable table)
        {
            bool tableAlreadyAdded = dataSet.Tables.Cast<DataTable>().Where(tbl => tbl.TableName == table.TableName).Any();
            if (tableAlreadyAdded)
            {
                foreach (DataRow currentRow in table.Rows)
                {
                    dataSet.Tables[table.TableName].ImportRow(currentRow);
                }
            }
            else
            {
                dataSet.Tables.Add(table);
            }
        }

UPDATE:

This solved my problem I added this to my app config file:

 <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

But STILL, my report is empty! Nothing to show!

Upvotes: 0

Views: 1570

Answers (1)

owczarek
owczarek

Reputation: 347

Try adding name to the table. In the template you use the table named TestDataTable but there is no such table in the DataSet (the preview shows it is named Table1).

Upvotes: 1

Related Questions