Setting the Datasource for a Local Report - .Net & Report Viewer

how to set Datatable as datasource in ReportViewer

It seems you have forgotten to set the report source for your report viewer control. You can set the report source using either of this options:

  • LocalReport.ReportEmbeddedResource : The name of the report-embedded resource.
  • LocalReport.ReportPath : The file system path of the local report.
  • LocalReport.LoadReportDefinition(Stream): Loads a report definition for processing using a Stream.
  • LocalReport.LoadReportDefinition(TextReader) Loads a report definition from the local file system using a TextReader.

For example, I suppose you have added a report to your project, so you can show it in the report viewer this way:

var reportDataSource1 = new ReportDataSource("NameOfReportDataSet", YourDataTable);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
this.reportViewer1.RefreshReport();

Also you can simply set the report of the report viewer using designer. Put a report viewer on your form and click on top-right arrow to open the smart tag window of report viewer, then choose a report from combo box.

enter image description here

When using Report Viewer and asked for datasource my data access class does not show up

What I ended up doing to get the report viewer to access my datatable was add a dataset to my project. Within that dataset I had to create a datatable with the same column names as in my code generated data table. From there I went to my report and added a table and pointed it towards the datatable which was created within the dataset. Then in my code I programmatically added the datatable using the code described above from Shelby.

Single Object as datasource for report viewer

It's very simple, but it's too long to write all the passages.

In short, you have to:

  1. Create your RDLC file from the report designer
  2. Bind your RDLC file with the DataSet you have
  3. (Option) Bind your report with the ReportViewer to show it on your webpage

The third passage is optional because you can also write code to render directly your report and put it in download as pdf file (or excel).

This blog's post explain a lot of passages so just have a look at it.
There is also the famousgotreportviewer website but it's a bit outdated (still has good examples)

Using .NET Class as the DataSource with SSRS RDLC

Here are a couple solutions, but I prefer the second one.

Solution 1 (okay)

As this appears to be a bug with the MVC Web Application project type itself, you can add the report to a different project type (like Class Library). As described in the post Visual Studio 2010 Report Viewer - Object Datasource, just create a separate library for the project and add the rdlc file there. The data source configuration wizard should now look like this:

data source configuration wizard

Solution 2 (better)

As figured out in Can't see or add Website Data Sources in RDLC report in ASP.NET MVC, you can just add an aspx page anywhere to the MVC project to trick Visual Studio into pulling in the right design time libraries.

Just do the following:

  • Close all windows
  • Clean & Rebuild Solution
  • Add WebForm1.aspx to the Project
  • Open up the RDLC file and choose a DataSource from the dropdown:

    DataSource Dropdown

rdlc report - ‘A data source instance has not been supplied for the data source’ error

Finally I found the cause, it was a non corresponding Tablix data set name property. I had to rename it from dataSet1 to dsProducts.

Datatable as datasource in ReportViewer

The overload you're using for the constructor of the ReportDataSource object is expecting the name of the data source in that first parameter. You're not supplying this, you need the DataTable name.

Update your code to this and you should be OK:

ReportDataSource source = new ReportDataSource("DataTable1", dt);

ASP.NET ReportViewer Datasource connectionstring not supplied from web.Config

You can do it programmatically:

ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));

dt would be your dataTable

EDIT:

<rsweb:ReportViewer ID="ReportViewer1" runat="server">
<LocalReport ReportEmbeddedResource="Report1.rdlc" ReportPath="Report1.rdlc">
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource>

Datasource for RDL reports with ReportViewer

Did you verify the DataSourceReference element in your RDL? It needs the path to the reporting server.

The DataSourceReference element can contain a full folder path (for
example, /SampleReports/AdventureWorks) or a relative path (for
example, AdventureWorks). Relative paths start in the same folder as
the report. The shared data source must be on the same server as the
report.

Verify the DataSourceID too. Take a look at the answer on this question. It looks like it might be the same problem you are having.

If you are using an RDLC you could also set the datasource of your report manually using ReportDataSource. "GetMyData" in the example below would implement IEnumerable or IDataSource.

ReportDataSource reportDataSource = new ReportDataSource("MyDataName", GetMyData(startAt, endAt));
ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reporting/MyReport.rdlc");

ReportParameterCollection col = new ReportParameterCollection();
ReportParameter startAtParam = new ReportParameter("StartAt", startAt.ToString("MMM, dd yyyy"));
col.Add(startAtParam);
ReportParameter endAtParam = new ReportParameter("EndAt", endAt.ToString("MMM, dd yyyy"));
col.Add(endAtParam);

ReportViewer1.LocalReport.SetParameters(col);

If you are converting an RDL to an RDLC you can follow the steps here. Note that you need to re-create data source and query information. Also, the XML schema definition is different between 2005 and 2008.



Related Topics



Leave a reply



Submit