2013-03-12 115 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