<style>
img[alt$="><"] {
  border: 1px solid lightgrey;
}

</style>

# Creating Master-Detail Reports with SubReports

Using [SubReports](https://www.telerik.com/products/reporting/documentation/wrd-user-guide/components/subreport-item/overview) in the Web Report Designer allows you to embed reports within other reports for creating master-detail hierarchies and reusable report components.

The following example demonstrates how to create a master-detail report using SubReports. The master report (*CategoriesProducts.trdp*) contains a table with Northwind **Categories** data. The SubReport (*ProductsReport.trdp*) displays Northwind **Products** records filtered by the respective `CategoryID`.

> To follow along with the steps below, you need access to an instance of the [Northwind](https://github.com/microsoft/sql-server-samples/tree/master/samples/databases/northwind-pubs) database for [Microsoft SQL Server](https://learn.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server?view=sql-server-ver17).

The next video demonstrates how to create master-detail reports by using SubReports in the Telerik Web Report Designer.

<iframe width="560" height="315" src="https://www.youtube.com/embed/GnZi9PP9EK8?si=qaBfIZnbUu1fQzyK&rel=0" title="Adding a SubReport Item in the Telerik Web Report Designer" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

## Step 1: Building the Child Report

1. Start with a blank report (*ProductsReport.trdp*) that will serve as the child report. Select the header and footer sections and delete them.

   ![SubReport ><](https://www.telerik.com/products/reporting/documentation/assets/2c11c4b43ffc17c77cf17adf983cb0ae/wrd-components-subreport-new-report.png)

2. Add a new [SQL Data Source](https://www.telerik.com/products/reporting/documentation/wrd-user-guide/components/data-sources) populated with the `Northwind.Products` table:

   Here is the SQL query that selects the respective data:

   ```SQL
   SELECT
           [dbo].[Products].[ProductID],
           [dbo].[Products].[ProductName],
           [dbo].[Products].[Discontinued],
           [dbo].[Products].[SupplierID],
           [dbo].[Products].[CategoryID],
           [dbo].[Products].[QuantityPerUnit],
           [dbo].[Products].[UnitPrice],
           [dbo].[Products].[UnitsInStock],
           [dbo].[Products].[UnitsOnOrder],
           [dbo].[Products].[ReorderLevel]
   FROM    [dbo].[Products]
   ```

3. Set the **DataSource** of the report using the created SQLDataSource:

   ![Report DataSource ><](https://www.telerik.com/products/reporting/documentation/assets/2d3a4e325971b8267434a150fac9474a/wrd-components-subreport-report-data-source.png)

4. From the **Components** tab, drag two **TextBox** report items to the Detail section:

   4.1. Select each of the TextBoxes.

   4.2. Bind the **Value** property of the TextBoxes to the `ProductName` and `UnitPrice` fields respectively.

   4.3. Use the **Expression** dialog to set the desired field.

   ![Bind Product Fields ><](https://www.telerik.com/products/reporting/documentation/assets/42641fe5ec6114cc829ce3b915bca23b/wrd-components-subreport-bind-product-fields.png)

5. Preview the report to verify that all Products are listed.

6. Add an integer report parameter called `ProductCategoryID` that you will use to filter by product:

   ![Create Report Parameter ><](https://www.telerik.com/products/reporting/documentation/assets/363f94f1fe2ceb10472da05a30dcbc27/wrd-components-subreport-create-report-parameter.png)

7. Update the `SELECT` query of the added SQLDataSource and add a `WHERE` clause using an SQL parameter (which is mapped to the previously created report parameter):

   ```SQL
   SELECT
           [dbo].[Products].[ProductID],
           [dbo].[Products].[ProductName],
           [dbo].[Products].[Discontinued],
           [dbo].[Products].[SupplierID],
           [dbo].[Products].[CategoryID],
           [dbo].[Products].[QuantityPerUnit],
           [dbo].[Products].[UnitPrice],
           [dbo].[Products].[UnitsInStock],
           [dbo].[Products].[UnitsOnOrder],
           [dbo].[Products].[ReorderLevel]
   FROM [dbo].[Products]
   WHERE [dbo].[Products].[CategoryID] = @sqlParamCategoryId
   ```

   ![Update SQL Select ><](https://www.telerik.com/products/reporting/documentation/assets/8a41fa183213af7cf43426d24f356740/wrd-components-subreport-update-sql-select.png)

8. Save the report (*ProductsReport.trdp*). You will use it as a child report.

## Step 2: Building the Master Report

1. Create a new blank report (*CategoriesProducts.trdp*).

2. Add a new [SQL Data Source](https://www.telerik.com/products/reporting/documentation/wrd-user-guide/components/data-sources) filled with the `Northwind.Categories` table:

   ```SQL
   SELECT
           [dbo].[Categories].[CategoryID],
           [dbo].[Categories].[CategoryName],
           [dbo].[Categories].[Description],
           [dbo].[Categories].[Picture]
   FROM    [dbo].[Categories]
   ```

3. Select the report's Detail section and use the [Table Wizard](https://www.telerik.com/products/reporting/documentation/wrd-user-guide/components/tables) to add a table bound to the just added SQLDataSource:

   ![Create Categories Table ><](https://www.telerik.com/products/reporting/documentation/assets/861484a3dfd07476c9fef91afa000adf/wrd-components-subreport-create-categories-table.png)

4. Select the **Picture** header and change its text to **Products**.

5. Select the data cell bound to the `Picture` field (`[=Fields.Picture]`) and add a **SubReport** item by selecting it from the **Components** tab:

   ![Adding SubReport ><](https://www.telerik.com/products/reporting/documentation/assets/3307fa2aba3364da3156e4f66238827d/wrd-components-subreport-adding-subreport.png)

6. Specify the **Inner Report** for the **SubReport** item and select the Uri option that refers to the previously created `ProductsReport.trdp`:

   ![Setup UriReportSource ><](https://www.telerik.com/products/reporting/documentation/assets/de220fdfaee94d901b01ce7553c4375f/wrd-components-subreport-urireportsource.png)

7. Set up the relation between the parent and child reports:

   7.1. Adjust the parameters.

   7.2. Link the SubReport parameter (`ProductCategoryID`) with the master report's `CategoryID` field.

   ![Setup ProductCategoryID Report Parameter ><](https://www.telerik.com/products/reporting/documentation/assets/549501be2f126bee04a140c0b227b5e7/wrd-components-subreport-setup-productcategoryid-report-parameter.png)

8. Save the changes and preview the master report. You will see all categories listed with the respective products for each category:

   ![Master-Detail hierarchy ><](https://www.telerik.com/products/reporting/documentation/assets/cad6ba0c8d4bd113f77bd10a57e52bd1/wrd-components-subreport-master-detail-hierarchy.png)

## See Also

* [Web Report Designer](https://www.telerik.com/products/reporting/documentation/designing-reports/report-designer-tools/web-report-designer/overview)
* [Data Sources](https://www.telerik.com/products/reporting/documentation/wrd-user-guide/components/data-sources)
