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
Open Visual Studio. On the File menu, point to New, and then select Project.
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.
In the Name box, type the name of the project: ReportWalkthrough.
In the Location box, enter the directory in which you want to save your project, or click Browse to navigate to it.
The Windows Forms Designer opens, showing Form1.vb of the project you created.
Define a data source connection and DataTable
On the Project menu, select Add New Item.
In the Add New Item dialog, click DataSet.
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.
In Dataset Designer, open the Toolbox, and drag a TableAdapter onto the DataSet design surface. This opens the TableAdapter Configuration Wizard.
On the Choose Your Data Connection page, click New Connection.
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.
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.
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.
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.
On the Choose the Command Type page, select Use SQL Statements, and then click Next.
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.
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
On the Project menu, select Add New Item.
In the Add New Item dialog, click Report Wizard.
Enter a name for the report file. By default, the report name is Report1.rdlc. Click Add.
This launches the Report Wizard.
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.
In the Arrange Fields page do the following:
Drag Dept from available fields to the Row groups box.
Drag Shift from available fields to the Column groups box.
Drag EmployeeID from available fields to the Values box.
Click the arrow next to Sum(EmplyeeID) and select Count.
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.
Save the .rdlc file.
Add a ReportViewer control to the application
Open the Form1.vb file in your project in design mode.
In the Form properties window, expand the Size property, then set the Height property to 700.
From the Reporting section of the Toolbox, drag the ReportViewer control to the form.
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.
In the open smart tags panel, choose Dock in parent container.
Build and run the application
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.
Press F5 to run the application and view the report in your form.