简体   繁体   English

尝试将数据集设置为Crystal报表源时出错

[英]Error when trying to set dataset to crystal report source

I have a physical dataset file in which I did this: 我有一个物理数据集文件在其中执行以下操作:

在此处输入图片说明

In the CR designer I set that dataset to a source like this: 在CR设计器中,我将该数据集设置为如下所示的源:

在此处输入图片说明

The problem I am facing in the c# code is at this line: 我在C#代码中面临的问题在这一行:

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: 其中document是一个预加载的CR文档,而rv是一个数据集,看起来像在上述行之前:

在此处输入图片说明

Finally, when I come to that line I get this error. 最终,当我到达那条线时,我得到了这个错误。 Not even exception is thrown... 甚至没有引发异常...

在此处输入图片说明

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! 什么都没显示!

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 ). 在模板中,使用名为TestDataTable表,但DataSet中没有这样的表(预览显示该Table1名为Table1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM