Walkthrough: Using a Database Data Source with the ReportViewer Windows Forms Control in Local Processing Mode

This walkthrough shows how to create a report in a Windows Forms Application project and how to add a ReportViewer control to a Windows Form so that the report can be viewed by users of the application.


To use this walkthrough, you must have access to the AdventureWorks2008 sample database. For more information, see Walkthrough: Installing the AdventureWorks Database.

Perform the following steps to add a report to a Windows Forms application project. For this example, you create the application in Microsoft Visual Basic.

Create a new Windows application project

  1. Open Visual Studio. On the File menu, point to New, and then select Project.

  2. In the Installed Templates pane, choose Visual Basic, and then choose Windows Forms Application. The Visual Basic node may be under Other Languages, depending on your startup settings in Visual Studio.

  3. In the Name box, type the name of the project: ReportWalkthrough.

  4. In the Location box, enter the directory in which you want to save your project, or click Browse to navigate to it.

  5. Click OK.

    The Windows Forms Designer opens, showing Form1.vb of the project you created.

Define a data source connection and DataTable

  1. On the Project menu, select Add New Item.

  2. In the Add New Item dialog, click DataSet.

  3. Enter a name for the dataset and click Add. This adds a new XML Schema file to the project and opens the Dataset Designer. The default name is DataSet1.xsd.

  4. In Dataset Designer, open the Toolbox, and drag a TableAdapter onto the DataSet design surface. This opens the TableAdapter Configuration Wizard.

  5. On the Choose Your Data Connection page, click New Connection.

  6. If this is the first time you create a data source in Visual Studio, you will see the Choose Data Source page. In the Data Source box, select Microsoft SQL Server.

  7. On the Add Connection page, perform the following steps:

    • In the Server name box, enter the server where the AdventureWorks2008 database is located.

      The default SQL Server Express instance is (local)\sqlexpress.

    • In the Log on to the server section, select the option that provides you access to the data. Use Windows Authentication is the default.

    • From the drop-down list, click AdventureWorks2008.

    • Click OK to continue to return to the wizard.

  8. If you specified Use SQL Server Authentication in the previous step, select the option whether to include the sensitive data in the string or set the information in your application code.

  9. Click Next.

  10. On the Save the Connection String to the Application Configuration File page, type in the name for the connection string or accept the default AdventureWorks2008ConnectionString. Click Next.

  11. On the Choose the Command Type page, select Use SQL Statements, and then click Next.

  12. On the Enter a SQL Statement page, enter the following Transact-SQL query to retrieve data from the AdventureWorks2008 database, and then click Finish.

    SELECT d.name as Dept, s.Name as Shift, 
        e.BusinessEntityID as EmployeeID
    FROM (HumanResources.Department d
    INNER JOIN HumanResources.EmployeeDepartmentHistory e
        ON d.DepartmentID = e.DepartmentID)
    INNER JOIN HumanResources.Shift s
        ON e.ShiftID = s.ShiftID

    You can also click on the Query Builder button and use Query Builder to create your query and check it using the Execute Query button. If the query does not return the expected data, you might be using an earlier version of AdventureWorks. For more information about installing the AdventureWorks2008 version of AdventureWorks, see Walkthrough: Installing the AdventureWorks Database.

  13. On the Choose Methods to Generate page, click Finish.

    On the DataSet design surface, you should see DataTable1 with the columns resulting from the query.

Design the report using the Report Wizard

  1. On the Project menu, select Add New Item.

  2. In the Add New Item dialog, click Report Wizard.

  3. Enter a name for the report file. By default, the report name is Report1.rdlc. Click Add.

    This launches the Report Wizard.

  4. In the Dataset Properties page, in the Data source box, select the DataSet you created.

    The Available datasets box is automatically updated with the DataTable you created.

  5. Click Next.

  6. In the Arrange Fields page do the following:

    1. Drag Dept from available fields to the Row groups box.

    2. Drag Shift from available fields to the Column groups box.

    3. Drag EmployeeID from available fields to the Values box.

    4. Click the arrow next to Sum(EmplyeeID) and select Count.

  7. Click Next twice, then click Finish.

    This creates the .rdlc file and opens it in Report Designer. The tablix you designed is now displayed in the design surface.

  8. Save the .rdlc file.

Add a ReportViewer control to the application

  1. Open the Form1.vb file in your project in design mode.

  2. In the Form properties window, expand the Size property, then set the Height property to 700.

  3. From the Reporting section of the Toolbox, drag the ReportViewer control to the form.

  4. Open the smart tags panel of the ReportViewer control by clicking the smart-tag glyph on the top right corner. Click the Choose Report drop-down list and select the report you created. Selecting a report causes instances of data sources used in the report to be created automatically. Code is generated to instantiate a DataSet (the ADO.NET container for a DataTable), a TableAdapter component, and a BindingSource object corresponding to each data source used in the report.

  5. In the open smart tags panel, choose Dock in parent container.

Build and run the application

  1. On the Build menu, click Build ReportWalkthrough. As part of the build process, the report is compiled. Some errors (such as a syntax error in an expression used in the report) are noted by adding them to the Task List.

  2. Press F5 to run the application and view the report in your form.

See Also




Using the ReportViewer Tasks Smart Tags Panel

Other Resources

Samples and Walkthroughs