2012-11-11 17 views
13

我試圖在報告中顯示記錄。數據在數據集中。但它不是綁在他們身上的。表單加載時顯示報表佈局。但是當我點擊按鈕時,它顯示錯誤。下面是 是我的代碼。數據源實例尚未提供給Microsoft報告服務中的數據源「Product_Detail」

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個表格。我在報表查看器頂部選擇一個箭頭顯示的綁定源。

回答

21

在使用Visual Studio.Net 2012編輯代碼時,我在使用ReportViewer的第10版時遇到了這個問題。

我找到了一個解決方案,在錯誤消息中取出數據源的名稱(在上面的例子中,它是「Product_Detail」)。然後我進入源代碼視圖,找到ReportViewer,它的DataSources,然後在它的ReportDataSource中。

我將ReportDataSource的Name屬性設置爲與錯誤消息(即「Product_Detail」)中提到的數據源相同。

我希望這對你有用,就像它爲我做的那樣。另外,如果您有使用ReportViewer控件的更高版本的自由度,您可能會發現此問題要麼不出現,要麼更容易解決。

9

「ProductsDataSet」是您要給它的DataSource的名稱。 您的錯誤提示「數據源實例尚未提供給Microsoft報告服務中的數據源」Product_Detail「」

我假設您爲其指定了錯誤的名稱。

嘗試,

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

如果你確實有所謂的「ProductsDataSet」那麼你可能有2報表數據源,在其中你想刪除不使用的一個或分配給它一個數據源也是如此。

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事件(包括報表查看器,並添加填寫新的數據集的報告。

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

我在C#應用程序就遇到了這個在VS2013 ..所以萬一別人得到你here..If添加在報表設計器的數據集..轉到您的形式,在德igner,單擊reportviewer控件上的操作箭頭。選擇重新綁定數據源。

+0

謝謝。現在,爲什麼微軟不能在他們的錯誤信息中提出這個建議。 –

相關問題