Scott Booth's Blog
My Links
Blog Stats
  • Posts - 5
  • Stories - 0
  • Comments - 10
  • Trackbacks - 0
Archives

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;

 

}

 

posted on Thursday, March 02, 2006 12:00 PM
Feedback
  • # re: Tips for Creating SubReports With Reporting Services in Asp.Net 2.0
    Jens
    Posted @ 4/12/2006 1:39 AM
    Hi Scott,

    I have problems using parameters in subreports. As soon as I add a parameter to one of my subreports the SubreportProcessingEvent doesn't get fired anymore. Do you have an idea how to solve the problem?
  • # re: Tips for Creating SubReports With Reporting Services in Asp.Net 2.0
    Jens
    Posted @ 4/12/2006 2:16 AM
    Hey Scott,
    I had to enable the "Allow null value" option of the parameter. Now it works.
    Although I don't have a clue of how to populate the parameter. I have problems using the example you provided in your post.
  • # re: Tips for Creating SubReports With Reporting Services in Asp.Net 2.0
    Scott
    Posted @ 4/12/2006 11:16 AM
    I'm using an object datasource to populate the report and subreport. I haven't tried this with a sql or xml datasource, so I'm not sure if it would work the same way. But I would assume so...

    In the SubreportProcessingEventHandler, the "event args" holds all parameters specified in the subreport control you added to the parent report. You can view/add these parameters that you want to pass from the parent report to the subreport by right clicking the subreport control and selecting properties. Then click on the parameters tab.

    In this tab, you can specify fields you've added to the parent reports (=Fields!ScheduleDate.Value). The parameter name must match the parameter name specified in subreport rdlc.

    You then have to add these parameters to your data source that your subreport uses. So I first call the LoadItemsData() to get my datasource. Then if there are parameters, I remove the first parameter because it is null and I can not reassign this value because it is read only. Then, I create a new parameter, assign the value of the event arg parameter to this new parameter, and add this new parameter to my data source.
  • # re: Tips for Creating SubReports With Reporting Services in Asp.Net 2.0
    roy
    Posted @ 12/8/2006 10:04 PM
    hi Scott,
    i want to show the subreport's page header in the report. how can i do that?
    thanks.
Title  
Name  
Url
Comments   
Protected by Clearscreen.SharpHIPIn order to prevent spam, please enter the code to post a comment.:
Scott Booth