How to display a report that contains a subreport by using the ReportViewer control in Visual Studio 2005

Article translations Article translations
Article ID: 919157 - View products that this article applies to.
Expand all | Collapse all

SUMMARY

In Microsoft Visual Studio 2005, you cannot directly display a report that contains a subreport by using the ReportViewer control in your application. When you try to display the report, you may receive the following error message:
Error: Subreport could not be shown
Additionally, you will receive the following warning message in the Visual Studio 2005 Output window:
Warning: An error occurred while executing the subreport ‘subreport1’: An error has occurred during report processing. (rsErrorExecutingSubreport)
This issue occurs when all the following conditions are true:
  • You try to show a report by using the ReportViewer control in your application.
  • The report contains a subreport.
  • The processing mode of the ReportViewer control is set to Local.
However, you can display the report by adding an event handler for the SubreportProcessing event of the ReportViewer control. Then, manually set the data source for the report in the event handler process. This article describes how to display a report that contains a subreport by using the ReportViewer control.

MORE INFORMATION

To use the ReportViewer control to display a report that contains a subreport in Local mode, follow these steps:
  1. Add an event handler for the SubreportProcessing event before you call the RefreshReport method. For example, if you want to display the report when the form is loaded, add the following code to the Load event. In this code example, the event handler for the SubreportProcessing event is SubreportProcessingEventHandler:

    Microsoft Visual Basic .NET code
    Private Sub <FormName>_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    	'
    	' Other code is here.
    	'
    	Dim instance As LocalReport = Me.ReportViewer1.LocalReport
    
    	Me.ContactTableAdapter.Fill(Me.AdventureWorksDataSet.Contact)
    
    	AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf Me.SubreportProcessingEventHandler
    	Me.ReportViewer1.RefreshReport()
    	'
    	' Other code is here.
    	'
    End Sub
    Microsoft Visual C# code
    private void <FormName>_Load(object sender, EventArgs e)
    {
    	/*
    	Other code is here.
    	*/
    
    	this.ContactTableAdapter.Fill(this.AdventureWorksDataSet.Contact);
    
    	this.reportViewer1.LocalReport.SubreportProcessing +=
      	new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
    	this.reportViewer1.RefreshReport();
    	/*
    	Other code is here.
    	*/
    }
    Note <FormName> represents the name of the form.
  2. Add the code for the event handler, and then manually set the data source for the report in the event handler process. For example, use the following code example.

    Note This code example assumes that you have added a data source to the project and bound the data source to the form. The subreport contains data from the data source. For more information about how to bind the data source to the form, visit the following Microsoft Developer Network (MSDN) Web site:
    http://msdn2.microsoft.com/en-us/library/h974h4y2(vs.80).aspx
    Visual Basic .NET code
    Public Sub SubreportProcessingEventHandler(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)
    
    	e.DataSources.Add(New ReportDataSource("AdventureWorksDataSet_Contact", Me.AdventureWorksDataSet.Contact))
    
    End Sub
    Visual C# code
    void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
    {
    	e.DataSources.Add(new ReportDataSource("AdventureWorksDataSet_Contact", this.AdventureWorksDataSet.Contact ));
    }

REFERENCES

For more information about the ReportViewer control, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms251671(VS.80).aspx

Properties

Article ID: 919157 - Last Review: May 31, 2006 - Revision: 1.2
APPLIES TO
  • Microsoft SQL Server 2005 Reporting Services
Keywords: 
kbhowto kbexpertiseadvanced kbsql2005bi kbsql2005rs kbinfo KB919157

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com