尚未为Microsoft报告服务中的数据源“Product_Detail”提供数据源实例

时间:2012-11-11 15:32:56

标签: c# reportviewer rdlc

我试图在报告中显示记录。数据位于数据集中。但它并不适合他们。表单加载时,它会显示报表布局。但是,当我点击按钮时,它显示错误。 下面是我的代码。

using Microsoft.Reporting.WinForms;
//------------------------------------------------------------------
// <copyright company="Microsoft">
//     Copyright (c) Microsoft.  All rights reserved.
// </copyright>
//------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReportsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Data.DataSet ds = GetDataSet();
            //reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
            ReportDataSource rds = new ReportDataSource("ProductsDataSet", ds.Tables[0]);
            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(rds);
            this.bindingSource1.DataSource = rds;
            this.reportViewer1.RefreshReport();
        }

        private System.Data.DataSet GetDataSet()
        {
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection("Data Source=DELL;Initial Catalog=Products;Integrated Security=True");
            sqlConn.Open();
            string sql= string.Format ( @"select o.[User], o.OrderDate, o.Quantity, o.OrderDetail, c.ShopName, c.[Address], c.City, c.Ph, p.* from dbo.Clients c,dbo.Product_Service o,Product_D p,Junction j where o.ClientId = c.ClientId
                            and o.ProductId  = j.ProductId 
                                and j.PCode = p.PCode
                                  and o.ClientId = 41
                                        and o.OrderDate='11/9/2012';");

            System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter(sql, sqlConn);
            System.Data.DataSet ds = new System.Data.DataSet();
            ad.Fill(ds);
            sqlConn.Close();
            return ds;
        }
    }
}

在我的数据集中,我有3个表。我在reportviewer顶部选择了一个小箭头显示的绑定源。

10 个答案:

答案 0 :(得分:31)

在使用Visual Studio.Net 2012编辑代码时使用ReportViewer的第10版时遇到了这个问题。

我通过在错误消息中获取数据源的名称找到了一个解决方案(在上面的例子中,它是“Product_Detail”)。然后我进入源代码视图,找到了ReportViewer,它的DataSources,然后是它的ReportDataSource。

我将ReportDataSource的Name属性设置为与错误消息中提到的数据源相同(即“Product_Detail”)。

我希望这对你有用,就像它对我一样。

此外,如果您有使用更高版本的ReportViewer控件的自由度,您可能会发现此问题不会出现或更容易解决。

答案 1 :(得分:11)

“ProductsDataSet”是您提供的DataSource的名称。 您的错误是说“Microsoft报告服务中的数据源”Product_Detail“尚未提供数据源实例”

我假设你给它指的是错误的名字。

尝试,

ReportDataSource rds = new ReportDataSource("Product_Detail", ds.Tables[0]);

如果您在报告中有一个名为“ProductsDataSet”的数据源,那么您可能有2个,其中您想要删除未使用的数据源或者也为其分配数据源。

答案 2 :(得分:7)

我在我的c#app中遇到了VS2013中的这个...所以万一其他人来到这里..如果你在报表设计器中添加了数据集..转到你的表单,在设计器中,单击reportviewer上的动作箭头控制。选择重新绑定数据源。

答案 3 :(得分:0)

    Dim rptDataSource As ReportDataSource
    Try
        With Me.ReportViewer1.LocalReport
            ReportViewer1.LocalReport.ReportPath = Application.StartupPath & "\RTFLS\Report1.rdlc"
            '.DataSources.Clear()
        End With
        Dim ds As New POAS.CustomersTotalPayment 
        Dim da As New POAS.CustomersTotalPaymentTableAdapters.PAYMENTSTATUSTableAdapter

        da.Fill(ds.PAYMENTSTATUS)

        rptDataSource = New ReportDataSource("CustomersTotalPayment", ds.Tables("PAYMENTSTATUS"))
        Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)

        Me.ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)
    Catch ex As Exception
        MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try


    Me.ReportViewer1.RefreshReport()

答案 4 :(得分:0)

如果在将报告添加到报告查看器后将其他表添加到xsd表单,则可能会出现此错误。

  1. 删除报告查看器并重新添加
  2. 将报告设置为报告查看器
  3. 现在转到表单的Load事件(包括报表查看器并添加Fill for new数据集。

    private void rptForm_Load(object sender, EventArgs e) {
      this.vwrpt_TableAdapter1.Fill(this.DataSet1.vwDataset);
    }
    

答案 5 :(得分:0)

默认情况下,在我的rdlc文件中创建了两个dataSet(不知道原因)。通过记事本打开RDLC文件,并删除了不需要的数据源。这些值两次分配给两个不同的数据集。 希望对您有帮助。

答案 6 :(得分:0)

导入Microsoft.Reporting.WebForms 局部类Rpt_reports     继承System.Web.UI.Page     受保护的子Page_Load(作为对象发送,作为EventArgs发送)处理Me.Load         如果不是IsPostBack然后             昏暗的ds作为新的客户             'da.Fill(ds.Tables(“ custamer”)             字符串为Dim路径= Server.MapPath(“ customerinfo.rdlc”)

        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.ReportPath = path
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("Rpt_reports", ds.Tables("Custamer")))
        ReportViewer1.LocalReport.Refresh()
    End If


End Sub

'Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'End Sub

结束班级

答案 7 :(得分:0)

我知道这篇文章有些陈旧,但我想与大家分享我的解决方案,以便对他们有所帮助。

在我的项目中,我遇到了相同的错误,即“ 未为数据源'dataset1'提供数据源实例”。我在项目中使用了代码优先的实体框架。

为了解决上述错误,我按照以下步骤操作。

  1. 选择报表设计器(报表查看器)上的操作箭头。
  2. 然后从选择报告下拉列表中选择RDLC报告。
  3. 选择“ 选择数据源Choose Data Source
  4. 然后选择所需的数据源Select datasource / binding source from the drop down in highlighted area

答案 8 :(得分:0)

我在VS 16.4.6和最新的报表设计器扩展15.3.1中遇到了这个问题。

对我来说,这是由于为数据集提供的名称与我从下拉菜单中选择的名称略有不同。我花了很长时间才弄清楚出了什么问题。直觉暗示这可能是一个问题。因此,我将数据集重命名为与源数据实体匹配,错误消失了。

但是为什么要让用户有机会调用其他数据集,然后不测试其是否有效呢?

报表查看器扩展程序的质量仍然很差。

答案 9 :(得分:0)

关于现有答案,我今天在VS2015 Professional中遇到了此问题,并使用操作箭头选择了正确的数据源并重新绑定,通过导航到Form1.cs(设计)来解决此问题。请查看下图,并按照红色,绿色和紫色指示进行操作,以查看我的解决步骤。

enter image description here