Configuring a data source for a local report - .NET & Report Viewer

I created my own control (window form with report viewer). I have the following code to download a local report:

Contained in the CustomReportViewer Class

//Load local report this.reportViewer1.ProcessingMode = ProcessingMode.Local; //enable loading of external images this.reportViewer1.LocalReport.EnableExternalImages = true; //pass the report to the viewer using (FileStream stream = new FileStream(filename, FileMode.Open)) { this.reportViewer1.LocalReport.LoadReportDefinition(stream); } 

I invoke this using:

 CustomReportViewer reportViewer = new CustomReportViewer(); 

This works fine and a window form appears containing the report viewer , but . I get the following message:

 A data source instance has not been supplied for the data source "ReportData" 

I'm not quite sure how to set up a data source? The necessary data is stored in a remote database ... what do I need to do to establish this connection?

+8
c # visual-studio visual-studio-2010 report
source share
2 answers

You need to create a ReportDataSource and set its Value property - for example, DataTable and IEnumerable are supported sources

As an example and assuming there is a method to return a DataSet , with a single DataTable corresponding to the columns needed for your report

 DataSet ds = SomeMethodToRetrieveDataSet(); // eg via DataAdapter // If your report needs parameters, they need to be set ... ReportParameter[] parameters = new ReportParameter[...]; ReportDataSource reportDataSource = new ReportDataSource(); // Must match the DataSource in the RDLC reportDataSource.Name = "ReportData"; reportDataSource.Value = ds.Tables[0]; // Add any parameters to the collection reportViewer1.LocalReport.SetParameters(parameters); reportViewer1.LocalReport.DataSources.Add(reportDataSource); reportViewer1.DataBind(); 

Note that it is often easier to just embed the RDLC in your assembly, rather than storing separate RDLC files. Do this by selecting Build Action in the RDLC as Embedded Resource , and then you can set the ReportEmbeddedResource property:

 reportViewer1.LocalReport.ReportEmbeddedResource = "MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc"; 

Note that the resource string must include the fully qualified name of the resource (including assembly).

+16
source share

The key for me was as StuartLC answered, as described above ... with further clarification that when he said that he "must match the DataSource in the RDLC" .. it actually turned out to be the value of the "DataSetName" re element: <DataSetName>DataSet1</DataSetName>

I went around because it is called "DataSource", so I continued to use the name of the DataSource element, but apparently in the rdl and rdlc file it really means DataSetName. Therefore, bearing in mind here, this is a code that was borrowed from Stuart above by my own. Note the value of the DataSetName element:

  using (SqlConnection sqlConn = new SqlConnection(rvConnection)) using (SqlDataAdapter da = new SqlDataAdapter(rvSQL, rvConnection)) { DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; this.reportViewer1.Reset(); this.reportViewer1.ProcessingMode = ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "ssrsExport.rdlc"; ReportDataSource reportDataSource = new ReportDataSource(); // Must match the DataSet in the RDLC reportDataSource.Name = "DataSet1"; reportDataSource.Value = ds.Tables[0]; this.reportViewer1.LocalReport.DataSources.Add(reportDataSource); this.reportViewer1.RefreshReport(); } 
+7
source share

All Articles