1

I'm using Wpf to View Some reports using Report Viewer .. I designed the a report using sql report builder 'ReportByTopic.rdl' added it to the solution .. the DataSet inside the rdl file named 'Dataset1' and there are three parameters @TopicId ,@SDate, @EDate tested the query and it's working fine

I am trying to load filterd data into ReportViewer but no data is viewed

and here's the code

private void reportViewer_load()
{
    ReportParameter[] Params = new ReportParameters[3];
    Params[0] = new ReportParameter("TopicId", "4");
    Params[1] = new ReportParameter("SDate", "2009-01-01");
    Params[2] = new ReportParameter("EDate", "2017-01-01");

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

    reportViewer.LocalReport.ReportPath = "Reports\ReportByTopic.rdl";
  reportViewer.LocalReport.DataSource.Add(rds)
    reportViewer.LocalReport.SetParameters(Params);
    reportViewer.Refresh();
}

so where did I go wrong

UPDATE

Based on the answers I made some changes to the above code. First I used a method 'GetData' which returns a datatable of all the data I need using the same query I used to design the rdl file . The edited code

    private void reportViewer_load()
{
    ReportParameter[] Params = new ReportParameters[3];
    Params[0] = new ReportParameter("TopicId", "4");
    Params[1] = new ReportParameter("SDate", "2009-01-01");
    Params[2] = new ReportParameter("EDate", "2017-01-01");

    ReportDataSource rds = new ReportDataSource("DataSet1", GetData());


    reportViewer.LocalReport.ReportPath = "Reports\ReportByTopic.rdl";
  reportViewer.LocalReport.DataSource.Add(rds)
    reportViewer.LocalReport.SetParameters(Params);
    reportViewer.Refresh();
}

the datatable gets filled correctly with 900 rows, but still the reportViewer doesn't

Amr Ibrahim
  • 167
  • 3
  • 14

1 Answers1

1

You omitted to pass the data to the report, when creating the ReportDataSource pass the DataSet name and the data to its constructor:

IEnumerable data = GetFromDataBase(...);
ReportDataSource rds = new ReportDataSource("DataSet1",data);

reportViewer.LocalReport.ReportPath = "Reports\ReportByTopic.rdl";
reportViewer.LocalReport.DataSource.Add(rds)

Take a look to ReportDataSource constructors for more details.

UPDATE:

From your update I spot a problem. You are invoking the Refresh method of the control base class, when you actually want to invoke RefreshReport. They are used for two different things. So your last four lines need to be replaced by this:

reportViewer.LocalReport.ReportPath = "Reports\ReportByTopic.rdl";
reportViewer.LocalReport.DataSources.Clear() //Added line
reportViewer.LocalReport.DataSource.Add(rds)
reportViewer.LocalReport.SetParameters(Params);
reportViewer.RefreshReport(); //Replaced line

Note that I first invoke Clear method before setting the datasource. This is useful when you want to reload/refresh report without closing it.

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • I specified the query in the rdl file's dataset .. doesn't it gets the data automatically using that query? – Amr Ibrahim Feb 21 '17 at 20:33
  • You only specified the schema for the report, but the actual data needs to be explicitly provided. – E-Bat Feb 21 '17 at 20:34
  • ok thank you for the answer .. but I would appreciate a little more help .. If the data is retrieved from various tables with many types and unable to be in a collection .. what is the proper object I put these data in – Amr Ibrahim Feb 21 '17 at 21:03
  • because I retrieved the data in a data table and it's still not working – Amr Ibrahim Feb 21 '17 at 21:29
  • The easiest way is to create a stored procedure to output the desire data. Then in code invoke the stored procedure and store the result in a datatable or another collection of your choice. Havee a look to http://stackoverflow.com/questions/1933855/how-can-i-retrieve-a-table-from-stored-procedure-to-a-datatable and http://blogs.msmvps.com/deborahk/dal-retrieve-a-datatable-using-a-stored-procedure/ – E-Bat Feb 21 '17 at 22:24
  • I've done that .. the data is loaded successfully into the datatable .. but still no records in the reportViewer. – Amr Ibrahim Feb 22 '17 at 08:51
  • Edit the question and post the new code you are using. – E-Bat Feb 22 '17 at 14:29