Walkthrough: Using a Business Object Data Source with the ReportViewer Web Server Control in Local Processing Mode

This walkthrough shows how to use an object data source in a report in a Microsoft Visual Studio 2008 ASP.NET application. For more information about Business objects and object data sources, see Binding to Business Objects.

Perform the following steps to add a report to a Visual Studio ASP.NET Web Site Project. For this example, you will be creating the application in Microsoft Visual C#.

Create a new ASP.NET Web site project

  1. On the File menu, point to New, and select Web Site.

  2. In the New Web Site dialog, choose Visual C# from the Language drop-down list, and choose the ASP.NET Web Site template.

  3. In Location, choose HTTP and type in the URL for the Web site. The default is https://localhost/WebSite. Click OK. The default web page opens in code view.

Create business objects to use as a data source.

  1. Select the project Web site in Solution Explorer (begins with http://). Right-click and choose Add New Item.

  2. In the Add New Item dialog, choose Class, type BusinessObjects.cs for the file name, and click Add.

  3. When prompted with the message "Would you like to place the class in the 'App_Code' folder," click Yes. The new file is added to the project and automatically opened in Visual Studio.

  4. Replace the default code for BusinessObjects.cs with the following code:

    using System;
    using System.Collections.Generic;
    
    // Define the Business Object "Product" with two public properties
    //    of simple datatypes.
    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;
            }
        }
    }
    
    // Define Business Object "Merchant" that provides a 
    //    GetProducts method that returns a collection of 
    //    Product objects.
    
    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;
        }
    }
    
  5. From the Build menu, select Build Solution. This creates an assembly for the object and allows the business object Product to appear in the Website Data Sources window after you add a report to the project.

Add a report to the project

  1. Make sure the project Web site or a project item is selected in Solution Explorer.

  2. Right-click on the project Web site and select Add New Item.

  3. In the Add New Item dialog, select Report. Type a name for the report and click Add. The report is added to the project and automatically opened in Report Designer. The default name for the report is Report.rdlc.

Check the Website Data Sources window

  1. Click the Report.rdlc[Design] tab. In the left pane, click the Website Data Sources tab. If the Website Data Sources tab is not visible, from the Data menu, select Show Data Sources.

  2. Confirm that the Product object and its two public properties, Name and Price, appear in a hierarchy in the Website Data Sources window.

Design the report

  1. With the report open in Design mode, open the Toolbox. From the Toolbox, drag a Table control onto the report. The table control opens in a tabbed Design window.

  2. From the Website Data Sources window, drag the Name field from the Product data source onto the first column of the Detail row of the table. The Detail row is the middle row. Notice that the Header row automatically fills in for you when you specify the Detail row.

  3. Drag the Price field onto the detail row of the second column, so that it is next to the Name field.

  4. (Optional) Select the header row of the table by clicking on the left table header icon and apply the Bold font style.

  5. Delete the unused column. Click the third column, then click on the header bar, and press the Delete key.

  6. To add a title to the report, open the Toolbox and drag a Textbox onto the report. Position the Textbox above the table. Type Products for the report name.

  7. (Optional) Apply font size and font style to the text to make the title stand out.

Add a ReportViewer control to the Web page

  1. Select the default Web page in Design view by right-clicking Default.aspx in Solution Explorer, and choose View Designer.

  2. Open the Toolbox. In the Toolbox, expand the Data node and drag the ReportViewer icon onto the Web page.

  3. Select the ReportViewer control, and open the smart tags panel by clicking the triangle on the top right corner. Click the Choose Report drop-down list and select the report you just designed. By default, the name is c:\inetpub\wwwroot\WebSite\Report.rdlc. Notice that an ObjectDataSource control appears immediately below the ReportViewer control, and is automatically set to retrieve the contents of the DataTable as configured through the data table's TableAdapter component.

Run the application

  • Press F5 to run with debugging on or CTRL + F5 to run without debugging, and view the report.

See Also

Reference

Microsoft.Reporting.WinForms.ReportViewer.Drillthrough
Microsoft.Reporting.WinForms.LocalReport.SubreportProcessing
Microsoft.Reporting.WebForms.ReportViewer.Drillthrough
Microsoft.Reporting.WebForms.LocalReport.SubreportProcessing

Concepts

Using the ReportViewer Tasks Smart Tags Panel

Other Resources

Samples and Walkthroughs