2013-03-12 72 views
1

我使用ReportViewer控件設計報表,但是當我運行該項目,我得到這個錯誤:的ReportViewer RDL文件

A data source instance has not been supplied for the data source 'DataSet1'.

這裏是我的代碼:

  SqlConnection myConnection = new SqlConnection(); 
      SqlCommand cmd = new SqlCommand(); 
      SqlDataAdapter sqla = new SqlDataAdapter(); 
      DataSet ds = new DataSet(); 
      DataTable dt = new DataTable(); 

      myConnection.ConnectionString = SqlDataSource1.ConnectionString; 

      cmd.Connection = myConnection; 
      cmd.CommandText ="select * from users"; 
      cmd.CommandType = CommandType.Text; 
      sqla.SelectCommand = cmd; 

      sqla.Fill(dt); 
      sqla.Fill(ds); 

      ReportViewer1.Reset(); 
      ReportViewer1.LocalReport.DataSources.Clear(); 
      ReportViewer1.Visible = true; 
      ReportViewer1.LocalReport.ReportPath = "reports/allusers.rdl"; 
      ReportDataSource rds = new ReportDataSource("ds_users",dt); 
      ReportViewer1.LocalReport.DataSources.Add(rds); 
      ReportViewer1.ZoomMode = ZoomMode.Percent; 
      ReportViewer1.LocalReport.Refresh(); 

什麼時我錯過了?

回答

1

我在創建rdl文件時添加了「DataSet1」,所以我需要傳遞此數據集的數據來呈現報表。我將ReportDataSource行更改爲以下內容:

ReportDataSource rds = new ReportDataSource(); 
rds.Name = "DataSet1"; 
rds.Value = dt; 

而且解決了錯誤消息。

+0

你可以接受你自己的答案。 – 2013-03-12 18:53:09

+0

直到2天之後,如果它不是問答形式。 – Shelby115 2013-03-13 18:06:13