A Problem when Dynamically Switch .rdlc in ReportViewer

Normally in a Windows Form project, you have multiple .rdlc template files. You can use a single ReportViewer control to dynamically bind the report file to multiple .rdlc files dynamically.

Please look at the following sample:

...
ReportParameter rp;
this.reportViewer1.LocalReport.DataSources.Clear();
try
{
    switch (reportType)
    {
        #region Analog Day Report
        case ReportType.AnalogDay:
            start_time = dtpDateStart.Value;
            AnalogReportObjList analogDailyRepObjList = new AnalogReportObjList();
            this.reportViewer1.LocalReport.ReportPath = this.baseRDLCFolder + "ReportAnalogDay.rdlc";
            rp = new ReportParameter("CompanyName", this.tbCompany.Text.Trim(), false);
            ReportParameter rpSelectedDate = new ReportParameter("SelectedDate", this.dtpDateStart.Value.ToString("yyyy-MMM-dd"));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp, rpSelectedDate }); 
            this.reportViewer1.LocalReport.DataSources.Add(
                new ReportDataSource("DataSet1", analogDailyRepObjList.GetAnalogDailyReportObjList(start_time, this.ListHostDBIndexSelected)));
            // Process and render the report
            this.reportViewer1.RefreshReport();
            break;
        #endregion
        
        #region Analog Shift Report
        case ReportType.AnalogShift:
            start_time = dtpDateStart.Value;
            AnalogReportObjList analogShiftRepObjList = new AnalogReportObjList();
            this.reportViewer1.LocalReport.ReportPath = this.baseRDLCFolder + "ReportAnalogShift.rdlc";
            rp = new ReportParameter("CompanyName", this.tbCompany.Text.Trim());
            ReportParameter rpShift = new ReportParameter("ShiftName", ((ListItem)this.cbShift.SelectedItem).Value.Trim());
            ReportParameter rpShiftTime = new ReportParameter("ShiftTime", this.dtpDateStart.Value.ToString("yyyy-MMM-dd HH:mm:ss"));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp, rpShift, rpShiftTime });  
            this.reportViewer1.LocalReport.DataSources.Add(
                new ReportDataSource("DataSet1", analogShiftRepObjList.GetAnalogByShiftReportObjList(
                    Convert.ToInt32(((ListItem)this.cbShift.SelectedItem).Value.Trim()),
                    start_time, 
                    this.ListHostDBIndexSelected)));
            // Process and render the report
            this.reportViewer1.RefreshReport();
            break;
        #endregion
        
        ...
    }
}
...

However, when you run above code, you will get the following error message:

“an error occurred during local report processing”

When you read detail information or debug, you will find another related error message:

“An attempt was made to set a report parameter ‘xxxxx’ that is not defined in this report” (xxxx is a Parameter name which you set in your .rdlc file)

No matter we tried to add “this.reportViewer1.LocalReport.DataSources.Clear();” in each case beginning , or load the Refresh() for reportViewer, or adjust code’s order, we still met the same error message.

There are not enough similar sample on internet, so we had to try to find reason ourselves. Finally, we found it is simple to resolve the issue.

We need to reset report viewer when start each case. add one line of code, then everything will be OK:

...
ReportParameter rp;

this.reportViewer1.Reset(); 

this.reportViewer1.LocalReport.DataSources.Clear();
try
{
    switch (reportType)
    {
      case ...
             ...
    }
    ...
}

How to repeat table header on every page in RDLC report ?

When you build a .RDLC report in Visual Studio 2010 just like following show, you will find the table header can not repeat on every page when you change pages.

repeatHeader00_0

Then you might select the table and go to its properties to set something, see below, there are two properties RepeatColumnHeaders and RepeatRowHeaders, you can set them to True.

But wait, Can they work ? NO! actually they doesn’t work, your table header still can not repeat on every page. Is it a bug ? This question should ask Microsoft. There was somebody report the similar issue, but today these 2 properties seems still no work. 

repeatHeader01

This is around way:  Put the table header to Page header section. Yes, this works. However, you have to adjust the space between table header and table data rows.

Is there other better solution?

Yes, we have. Please go back to your Visual Studio RDLC design screen, please look at the right bottom corner in design part. There is a small black rectangle there. Click it and check what you can see:

repeatHeader00

Yes, you can see “Advanced Mode”, please check on it.

repeatHeader02

After you check on Advanced Mode, you will see Row Groups and Column Groups like below:

repeatHeader03

Click “Static” in Row Groups list, and check the properties on the right side:

Set “RepeatOnNewPage” to “True”.

repeatHeader04

Save your works and build. and preview or view your report, you will see the table repeat on every page now.

Paper Sizes List

For printing module development, we list common paper sizes here.

A4_Size_Paper

ISO paper sizes

Format A A B B C C
Size mm in mm in mm in
0 841 × 1189 33.11 × 46.81 1000 × 1414 39.37 × 55.67 917 × 1297 36.10 × 51.06
1 594 × 841 23.39 × 33.11 707 × 1000 27.83 × 39.37 648 × 917 25.51 × 36.10
2 420 × 594 16.54 × 23.39 500 × 707 19.69 × 27.83 458 × 648 18.03 × 25.51
3 297 × 420 11.69 × 16.54 353 × 500 13.90 × 19.69 324 × 458 12.76 × 18.03
4 210 × 297 8.27 × 11.69 250 × 353 9.84 × 13.90 229 × 324 9.02 × 12.76
5 148 × 210 5.83 × 8.27 176 × 250 6.93 × 9.84 162 × 229 6.38 × 9.02
6 105 × 148 4.13 × 5.83 125 × 176 4.92 × 6.93 114 × 162 4.49 × 6.38
7 74 × 105 2.91 × 4.13 88 × 125 3.46 × 4.92 81 × 114 3.19 × 4.49
8 52 × 74 2.05 × 2.91 62 × 88 2.44 × 3.46 57 × 81 2.24 × 3.19
9 37 × 52 1.46 × 2.05 44 × 62 1.73 × 2.44 40 × 57 1.57 × 2.24
10 26 × 37 1.02 × 1.46 31 × 44 1.22 × 1.73 28 × 40 1.10 × 1.57

 

North American paper sizes

size in mm
Letter 8.5 × 11 216 × 279
Legal 8.5 × 14 216 × 356
Junior Legal 8.0 × 5.0 203 × 127
Ledger 17 × 11 432 × 279
Tabloid 11 × 17 279 × 432

Pass parameter to a .rdlc report file

How to pass a parameter to a .rdlc file :

Step:

1: In Visual Studio 2010, open your .rdlc file, and open “Report Data” window (If you can not see this window, go to View menu to open it);

2: Right click the “Parameters” node, and add a new Parameter, ie: named it “content“;

3: In your .rdlc file, add a textbox, named it tbContent, and set its filed express to :

=Parameters!content.Value

4: Go to your Form file which include your reporterview control, and add the following code:

           this.reportViewer1.LocalReport.ReportEmbeddedResource = “TestReport.Report1.rdlc”;
            ReportParameter rp = new ReportParameter(“content”, this.textBox1.Text);
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
            this.reportViewer1.RefreshReport();

5: then you can pass the parameter from the TextBox on the form to .rdlc file;

Hide a record in RDLC

Here we found a solution for hiding a record in .rdlc file:

Add the following expression in the Hidden property for the row.

=iif((Fields!Etag.Value.ToString().IndexOf(“E”)=0),true,false).

This expression will hide the records that are starts with “E”.

About update dataset for a .RDLC file in Visual Studio 2010

In Visual Studio 2010, if you open a report template file .rdlc. sometimes you might find you can not update dataset if your data source changed.

The most of possible reason is you did not see the “Report Data” panel in your Visual Studio 2010, see the below screen :

IF you can not see this panel, you should open it in the “View” menu:

VS 2010 Bug ! MS ReportViewer with Nested Objects Data Sources

There is a big bug in Visual Studio 2010 if you use MS ReportViewer and .rdlc file with Nested Objects Data Sources.

In old VS version , the Nested Objects data source worked. Just read this article first:

Sample on ReportViewer controls with Object data sources containing nested objects

Tudortr made it worked in 2006:

For example, Here we have a class:

    public class HUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }

    public class AnalogReportObj
    {
        public int PointNumber { get; set; }
        public HUser ManUser { get; set; }

    }

then you can use “=Fields!AnalogReportObj.ManUser.Value.LastName” in .RDLC file textbox to get nested object value . But in Visual Studio 2010, the above syntax to access nested properties no longer works. Microsoft stuff said they will fix this bug in VS 2010 SP1.

Microsoft HotFix:
Accessing Nested Objects in Data Source of Local Report Does Not Function

Brian Hartman’s Report Viewer Blog: Nested Objects in Local Mode

For old version, some friend had his experience if doesn’t work :
Microsoft reports nested object datasource gives #Error

Microsoft Reporter with Business Object

Microsoft Reporter does not only support database, but also a business object. You can create class as your data source of report RDLC file.

Here is a sample from MSDN:

using System;
using System.Collections.Generic;

public class Product {
    private string m_name;
    private int m_price;

    public Product(string name, int price) {
        m_name = name;
        m_price = price;
    }

    public string Name {
        get {
            return m_name;
        }
    }

    public int Price {
        get {
            return m_price;
        }
    }
}

public class Merchant {
    private List<Product> m_products;

    public Merchant() {
        m_products = new List<Product>();
        m_products.Add(new Product(“Pen”, 25));
        m_products.Add(new Product(“Pencil”, 30));
        m_products.Add(new Product(“Notebook”, 15));
    }

    public List<Product> GetProducts() {
        return m_products;
    }
}

Add a report to the project using the Report Wizard

   1.      From the Project menu, select Add New Item.
   2.      In the Add New Item dialog, select Report Wizard. Type a name for the report and click Add.
      This launches the Report Wizard with the Data Source Configuration Wizard.
   3.      In the Choose a Data Source Type page, select Object and click Next.
   4.      In the Select the Data Objects page, expand the class hierarchy under BusinessObject until you see Product in the list. Select Product and click Finish.
      You now return to the Report Wizard. Notice that the new data source object is added to your project in Solution Explorer.
   5.      In the Dataset Properties page, in the Data source box, verify that global is selected.
   6.      In the Available datasets box, verify that Product is selected.
   7.      Click Next.
   8.      In the Arrange Fields page do the following:
         1.            Drag Name from available fields to the Row groups box.
         2.            Drag Price from available fields to the Values box.
   9.      Click Next twice, then click Finish.
      This creates the .rdlc file and opens it in Report Designer. The tablix you created is now displayed in the design surface.
  10.      Save the .rdlc file.

from MSDN