【问题标题】:.rdlc Report - Cannot create a data reader for dataset 'DataSet1'.rdlc 报告 - 无法为数据集“DataSet1”创建数据读取器
【发布时间】:2013-03-12 15:46:57
【问题描述】:

我使用报表向导在 VS 2012 下创建了一个 .rdlc-Report 并添加了数据源和数据集。 当我尝试使用下面的代码呈现报告时,我收到以下错误消息:

无法为数据集“DataSet1”创建数据读取器。

bytes = localReport.Render("PDF", sdeviceinfo, out smimetype, out sencoding, out sfilenameextension, out streamids, out myWarnings);

【问题讨论】:

    标签: c# .net reporting-services visual-studio-2012 report


    【解决方案1】:

    我有同样的问题“无法为数据集'zzz'创建数据阅读器”

    答案是ReportDataSource(string xxx, DataTable yyy)

    您应该使用正确的名称。 xxx应该是zzz

    【讨论】:

    • 别忘了localReport.DataSources.Add() ReportDataSource -- 那是我的问题。
    • 但是如果xxx应该是zzz,那么yyy呢?应该是www吗?
    • @Rich yyy 是包含您的列的数据表
    【解决方案2】:

    我的“陷阱”是发现 DataSet 与 Dataset 不同。

    【讨论】:

    • 在我的情况下,我传递的是 Dataset1 而不是 DataSet1
    【解决方案3】:

    你必须给“DataSet1”,否则它不起作用。

    【讨论】:

    • 数据集和数据集不一样。正确重命名将解决错误。
    【解决方案4】:

    您无法创建 ReportViewer 并提供真实的数据集。由于参数提供技术,您可能会收到错误

    你可以这样试试;我用这种方式解决了:

       protected void btnPdf_Click(object sender, EventArgs e)
        {
        ReportViewer viwer = new ReportViewer();
        ObjectDataSource ob = new ObjectDataSource("dataset.YourTableAdapter", "GetData");
        dataset.YourTableAdapter ds = new dataset.YourTableAdapter();
    
        string PDF = "PDF";
        string ReportType = "ReportType";
        Warning[] warnings = null;
        string[] streamIds = null;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        string filetype = string.Empty;
    
       
    
        viwer.SizeToReportContent = true;
        viwer.LocalReport.ReportPath = "reports/report/report.rdlc";
        viwer.ProcessingMode = ProcessingMode.Local;
        ob.SelectParameters.Clear();
        ob.SelectParameters.Add(QueryStringEnum.CompanyID, CurrentCompanyID.ToString());
     
        ReportDataSource rds = new ReportDataSource("datasetname", (object) ds.GetData((long?)CurrentCompanyID.ToInt64());
    
        viwer.LocalReport.DataSources.Add(rds);
        viwer.LocalReport.Refresh();
    
        byte[] bytes = viwer.LocalReport.Render("PDF", null,
         out mimeType, out encoding, out extension, out streamIds, out warnings);
       
    
    
    
    }
    

    【讨论】:

      【解决方案5】:
      1. 确保您在管理员模式下运行并且您可以访问 SSRS 服务器。

      2. 验证您是否设置了正确的数据集名称,或者您是否正确加载和分配它。

      请查看 MSDN 上的this 示例。

      【讨论】:

        【解决方案6】:

        我解决了问题——我的编码错误——我从 SqlServerDataSource 的 aspx 代码中省略了“正确”参数值,如下所示...

        请注意,parameter-name="p_CSV_VEHICLES" 没有 DefaultValue

        并且(事实证明)这两个参数(="p_CSV_VGROUPS" 和 ="p_CSV_VEHICLES")无法将空字符串“”作为默认值传递——空字符串在这些参数的选择上下文中无效两个参数。

        在我添加 DefaultValue 并将 DefaultValue(s) 设置为每个参数的有效字符串值后,报告在我的网页上的 ReportViewer 控件中完美显示。

        这是原始的(不工作的)aspx 代码。

             <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FCI_WebMainConnectionString %>" 
                SelectCommand="uspRPT_CostDetailFleet" SelectCommandType="StoredProcedure">
                <SelectParameters>
                   <asp:Parameter DefaultValue="5" Name="p_UID_DIVISION" Type="Int32" />
                   <asp:Parameter DefaultValue="85" Name="p_UID_CUSTOMER" Type="Int32" />
                   <asp:Parameter DefaultValue="FCIFLEETGRP" Name="p_SORT_ORDER" Type="String" />
                   <asp:Parameter DefaultValue="" Name="p_CSV_VGROUPS" Type="String" />
                   <asp:Parameter Name="p_CSV_VEHICLES" Type="String" />
                   <asp:Parameter DbType="Date" DefaultValue="#01/01/2013#" Name="p_dtStart0" />
                   <asp:Parameter DbType="Date" DefaultValue="#12/31/2013#" Name="p_dtEnd0" />
                </SelectParameters>
             </asp:SqlDataSource>
        

        【讨论】:

          【解决方案7】:

          我也遇到了同样的问题。经过我的观察,我发现,RDLC 报告确实需要数据集,要么它应该有一些记录,要么是空对象(空对象将包含元数据)

          因此,最佳做法始终是返回一个包含空对象(不是 Nullnothing)或其中包含一些记录的数据集。

          【讨论】:

            【解决方案8】:
            reportviewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", ObjectDataSource1))
            

            最好发布你的代码,你如何将你的数据源设置到报告中。

            【讨论】:

              【解决方案9】:

              确保这两件事都OK:

              1) 在 Report1.rdlc[Design] 中(按 Alt+Ctrl+D):

              右键单击数据集并添加数据集,

              在数据集属性表单中,选择名称:DataSet1

              单击 DataSource combocox 旁边的 New... 按钮

              点击对象

              关于您的项目。选择 yourList1 或 ...

              然后完成。

              现在选择组合框上的数据源和可用数据集。

              重复这些步骤并创建 DataSet2 ...

              单击“确定”并关闭表单。

              2) 在您的FormPrint 代码中:

                  private void frmPrint_Load(object sender, EventArgs e)
                  {
                      CreateReport1();
                      System.Drawing.Printing.PageSettings ps = new 
                             System.Drawing.Printing.PageSettings();
                      ps.Margins.Top = CentimeterToPixel(0.9);
                      ps.Margins.Left = CentimeterToPixel(0.9);
                      ps.Margins.Right = CentimeterToPixel(0.9);
                      ps.Margins.Bottom = CentimeterToPixel(0.9);
                      ps.Landscape = false;
                      ps.PaperSize =new PaperSize ("A4", 827, 1169);
                      ps.PaperSize.RawKind = (Int32)(System.Drawing.Printing.PaperKind.A4);
                      //psize.RawKind = (int)PaperKind.A4;
                      //ps.PaperSize = psize;
                      reportViewer1.SetPageSettings(ps);
                      this.reportViewer1.RefreshReport();
                      this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                      WindowState = FormWindowState.Maximized;
                  }
                  private int CentimeterToPixel(double Centimeter)
                  {
                      int  pixel = -1;
                      using (Graphics g = this.CreateGraphics())
                      {
                          pixel =Convert.ToInt32 ( Centimeter * (g.DpiY / 2.54));
                      }
                      return pixel;
                  }
                  private void CreateReport1()
                  {
                      reportViewer1.LocalReport.DataSources.Clear();
                      reportViewer1.ProcessingMode = 
                                   Microsoft.Reporting.WinForms.ProcessingMode.Local;
              
              
                      reportViewer1.LocalReport.ReportEmbeddedResource = 
                                      "yourProjectName.Report1.rdlc";
                      ReportDataSource RDS = new ReportDataSource();
                      RDS.Name = "DataSet1";
                      RDS.Value = yourPublicList1;
                      reportViewer1.LocalReport.DataSources.Add(RDS);
              
                      ReportDataSource RDS2 = new ReportDataSource();
                      RDS2.Name = "DataSet2";
                      RDS2.Value = yourPublicList2;
                      reportViewer1.LocalReport.DataSources.Add(RDS2);
                  }
              

              【讨论】:

                【解决方案10】:

                请检查您的数据集名称是否正确。

                 reportDataSource = new ReportDataSource
                    {
                       Name = "DataSet1",// Check This Line. Are You Sure DataSet Name is Correct?
                       Value = ListData 
                    };
                

                【讨论】:

                • 已经有很多答案说“确保你的名字是正确的”。请仅在您有要添加的内容时添加答案。
                【解决方案11】:

                我无法分享源代码,但我想提交一个对我有帮助的答案。

                我从现有代码库中复制了一份之前制作的报告。它已经在 Visual Studio 的“报表数据”视图中创建了数据集,因为我复制了一个现有报表。 我需要这份已有报告的大部分内容;但是,我删除了其中一个数据集,并将其替换为我创建的一个新数据集,其中包含新报告所需的新数据。

                我在这里犯的一个大错误是我认为我将新数据集重新命名为与旧数据集完全相同的名称。旧数据集被命名为“ReportDetail”。但是,我新建的名为“ReportDetails”,带有“s”。

                我需要旧数据集中的一些数据字段;但是,我没有意识到那些旧字段仍在旧数据集名称下的复制报告中。所以在报告中有一个字段仍然被称为“ReportDetail.{fieldName}”。最终这就是问题所在。

                打开 .rdlc 报告的 XML 版本后,我可以清楚地看到旧的数据集名称仍在使用中。由于该旧数据集不再存在,因此引发了此错误。一旦我翻转该字段以使用新的数据集名称,它就可以正常工作。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-08-12
                  • 2020-01-12
                  • 2019-02-16
                  • 1970-01-01
                  • 2020-04-07
                  • 2011-07-14
                  • 2019-01-02
                  • 2014-08-20
                  相关资源
                  最近更新 更多