Creating subreports in asp.net 2.0 proved to be very difficult since there is limited documentation on it. If you don't have all the properties and necessary objects and values just right, you will receive the uninformative message “Error: Subreport can not be shown.“.
This error can mean lots of things. Check your stored procedure and data layer to insure that all the objects variables are correct. Then check all your parameter names, data source names, and report names to ensure they match.
The MSDN is a good starting place to see how to create subreports. Below is some useful tips that aren't explained in the MSDN.
In the report viewer of the main report, the subreport Name property must be the exact filename of the subreport minus the file extension. So if your subreport is “MySubReport.rdlc“, you would specify the name as “MySubReport“.
If you subreport takes any parameters, the parameter name in your subreport rdlc file must match the parameter name in the subreport in the report viewer. To view/create these parameters. right click on the subreport in the report viewer, click the parameter tab. Type the name of the param and specify the value for it. If you are using a field from the report, depending on the data type and param data type, you might need to append “.ToString()“ in order to get an acceptable data type.
One of the keys to using subreports is dynamically adding the data source for the subreport to use. To do this, add an event handler in the page load for the SubreportProcessing Event:
ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(ItemsSubreportProcessingEventHandler);
In the “ItemsSubreportProcessingEventHandler“, you can create your data source. The first argument in the ReportDataSource must be the data source name specified in your rdlc file.
If you are passing parameters to you subreport, you must specify them in your data source as well. The SubreportProcessEventArgs has a property that stores the parameters pased to the subreport. You can capture these parameters and add them to your data source parameter list.
void ItemsSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
if (odsItems == null)
odsItems = LoadItemsData();
if (e.Parameters.Count > 0)
{
if (odsItems.SelectParameters.Count > 0)
odsItems.SelectParameters.RemoveAt(0);
odsItems.SelectParameters.Add(new Parameter("JobId", TypeCode.String, e.Parameters[0].Values[0].ToString()));
}
e.DataSources.Add(new ReportDataSource("LandscapeItemsInstalledReport", odsItems));
}
private ObjectDataSource LoadItemsData()
{
ObjectDataSource ods = new ObjectDataSource();
ods.TypeName = "Gpe.Operations.Business.LandscapeItemsInstalledBC";
ods.SelectMethod = "GetSodTicketItems";
return ods;
}