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

问题描述 投票:18回答:8

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

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顶部选择了一个小箭头显示的绑定源。

c# reportviewer rdlc
8个回答
31
投票

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

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

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

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

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


11
投票

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

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

尝试,

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

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


7
投票

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


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()

0
投票

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

  1. 删除以报告查看器并再次添加
  2. 将报告设置为报告查看器
  3. 现在转到表单的Load事件(包括报表查看器并添加Fill for new数据集)。 private void rptForm_Load(object sender, EventArgs e) { this.vwrpt_TableAdapter1.Fill(this.DataSet1.vwDataset); }

0
投票

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


0
投票

导入Microsoft.Reporting.WebForms Partial Class Rpt_reports Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object,e As EventArgs)处理Me.Load如果不是IsPostBack然后Dim ds作为新的Custamer'da.Fill(ds.Tables) (“custamer”)Dim path As String = 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

结束班


0
投票

我知道这个帖子有点旧,但我想与你们分享我的解决方案,所以它可能有所帮助。

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

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

  1. 在报表设计器上选择操作箭头(报表查看器)。
  2. 然后从选择报告下拉列表中选择RDLC报告。
  3. 选择“选择数据源”Choose Data Source
  4. 然后选择您想要的数据源Select datasource / binding source from the drop down in highlighted area
© www.soinside.com 2019 - 2024. All rights reserved.