Setting the datasource for a Local Report - .NET & Report Viewer
Asked Answered
C

3

11

I have created a custom control (a windows form with a report viewer). I have the following code to load a local report:

Contained in 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 call this using:

CustomReportViewer reportViewer = new CustomReportViewer();

This works fine and a windows form appears containing the report viewer control but I get the following message:

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

I'm not entirely sure how to set up the data source? The data I require is stored in a remote database...what do I have to do to set this connection up?

Casares answered 10/1, 2012 at 11:41 Comment(0)
J
17

You need to create a ReportDataSource, and set its Value property - e.g. DataTable and IEnumerables are supported sources

As an example, and assuming that a method exists to return a DataSet, with a single DataTable matching the columns needed by your report:

DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. 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 into your assembly, rather than having to retain separate RDLC files. Do this by selecting the Build Action on 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).

Jenijenica answered 10/1, 2012 at 11:59 Comment(2)
Also note if your Resources are in a Folder, that the Folder name also gets into the fully qualified name.Jenijenica
Does anyone have a guide as to how to use the Data Source output in Field Expressions within the report?Margarethe
W
8

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

I went round and round because it is called "DataSource" so I kept using the DataSource element name but apparently in the rdl and rdlc file this really signifies the DataSetName. So keeping that in mind here is the code as borrowed from Stuart above with my own. Note the DataSetName element value:

        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();
        }
Worl answered 26/4, 2014 at 21:51 Comment(0)
R
0

you may be able to just use a datatable instead of a dataset. change this:

DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

to this:

DataTable dt = new DataTable();
da.Fill(dt);

and this:

reportDataSource.Value = ds.Tables[0];

to this

reportDataSource.Value = dt;
Ruskin answered 8/3, 2019 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.