C# ReportViewer 未指定报表定义的来源

Posted

技术标签:

【中文标题】C# ReportViewer 未指定报表定义的来源【英文标题】:C# ReportViewer The source of the Report Definition has not been specified 【发布时间】:2021-11-08 07:38:00 【问题描述】:

请帮我解决这个问题

我有一个与 SQL 服务器连接的 C# 项目 我想预览这个项目中的报告,但不知道为什么我的代码不起作用

//ReportDataSource reportDataSource;
try

    // Set the processing mode for the ReportViewer to Remote  
    reportViewer1.ProcessingMode = ProcessingMode.Remote;

    ServerReport serverReport = reportViewer1.ServerReport;

    // Get a reference to the default credentials  
    System.Net.ICredentials credentials =
        System.Net.CredentialCache.DefaultCredentials;

    // Get a reference to the report server credentials  
    ReportServerCredentials rsCredentials =
        serverReport.ReportServerCredentials;

    // Set the credentials for the server report  
    rsCredentials.NetworkCredentials = credentials;

    //this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\Report1.rdlc";
    this.reportViewer1.LocalReport.DataSources.Clear();

    DataSet1 dataSet1 = new DataSet1();
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
    
    con.Open();
    sqlDataAdapter.SelectCommand = new SqlCommand(@"SELECT 
                                                    dbo.Sales.product_name, dbo.Sales.price_dollar, dbo.Sales.price_dinar, dbo.Sales.quantity, dbo.Sales.totoal_dollar, dbo.Sales.total_dinar
                                                    FROM   dbo.Customers INNER JOIN
                                                    dbo.SaleReport ON dbo.Customers.customer_id = dbo.SaleReport.customer_id INNER JOIN
                                                    dbo.Sales ON dbo.SaleReport.trans_no = dbo.Sales.trans_no
                                                    where Sales.trans_no = '" + 1 + "';", con);
    sqlDataAdapter.Fill(dataSet1.Tables["Products"]);
    con.Close();

    ReportDataSource dsSalesOrder = new ReportDataSource();
    dsSalesOrder.Name = "DataSet1";
    dsSalesOrder.Value = dataSet1.Tables["Products"];

    //reportDataSource = new ReportDataSource("DataSet1", dataSet1.Tables["Products"]);

    reportViewer1.LocalReport.DataSources.Clear();
    reportViewer1.LocalReport.DataSources.Add(dsSalesOrder);
    reportViewer1.LocalReport.ReportEmbeddedResource = @"Report2.rdlc";
    reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
    reportViewer1.ZoomMode = ZoomMode.Percent;
    reportViewer1.ZoomPercent = 100;
    reportViewer1.LocalReport.Refresh();
    reportViewer1.RefreshReport();

catch (Exception ex)

    con.Close();
    MessageBox.Show(ex.Message);
    throw;

【问题讨论】:

如果您继续这样做,多次重复问题中的相同文本将被标记为垃圾邮件。 【参考方案1】:

就像错误所说的那样,

报告定义的来源尚未指定

您需要先指定rdlc模板文件,然后才能添加参数或生成报告。

我认为你只是调用了错误的方法, 行:

reportViewer1.LocalReport.ReportEmbeddedResource = @"Report2.rdlc";

实际上应该是:

reportViewer1.LocalReport.ReportPath = @"Report2.rdlc";

【讨论】:

以上是关于C# ReportViewer 未指定报表定义的来源的主要内容,如果未能解决你的问题,请参考以下文章

C# + asp.Net + 自定义 Sybase SessionState 提供程序 + ReportViewer = 一些报告崩溃

[C#]透过ReportViewer将报表另存成文件

C# Winform reportViewer报表,动态表头能做么?

c#语言,winform生成reportviewer报表,如何动态选择要显示的字段,就是选哪几列就

[ASP NET MVC] 使用ReportViewer执行用户端报表定义文件(.rdlc)产出报表

c#的reportViewer控件