• Aucun résultat trouvé

Rendering the Report Locally

Dans le document Pro SQL Server 2005Reporting Services (Page 182-186)

We’ll now cover how to render the report locally without using an SSRS 2005 server. It is a bit more complicated because you’re responsible for filling the data sources with the data that your reports require. However, it does provide tremendous flexibility to the developer because you can use SSRS 2005 reporting features without a server.

Caution

The server and local report definition files are not 100 percent compatible. This means it is not possible to use the same RDL source file for both local and remote use. In fact, you will note that by default reports for server-side or remote processing are created with the .rdlextension and reports for client-side or local processing are created with the .rdlcextension. While they are not 100 percent interchangeable, you can use one for the other with some changes to the underlying RDL.

Creating the Report’s Data Source

First you will add a data source to your project by adding the dataset you created previously.

To add the data source, follow these steps:

1. In the Project Explorer, right-click the project name SSRS Viewer RVC, point to Add, and select Existing Item.

2. Navigate to the folder containing the Chapter 6 examples, and select EmployeePay.xsd in the SRSS Viewer RVCfolder. This is the XML schema file you’ll use to define your data source for the report. When you add the .xsdfile to your project, it will also appear under the Data Sources window. If you don’t see the Data Sources window, select Data

➤Show Data Sources from the menu.

Note

Alternatively, you could have used the Data Source Configuration Wizard found by selecting Data ➤ Add New Data Source. With it you can create several types of data sources including those sourcing from a database, a Web service, or even an object. Just remember that much like this example, it is up to you to retrieve the data and populate the data source with it.

Next you’ll create a local RDL (.rdlc) that you’ll render using the ReportViewer control in local processing mode.

Designing the Report

To design the report, follow these steps:

1. To add a report to the project, right-click the project name—SSRS Viewer RVC—in the Solution Explorer.

2. On the shortcut menu, select Add ➤New Item. This opens the Add New Item dialog box.

3. Click the Report icon, enter EmployeePay.rdlc for the file name, and then click Add.

This launches the Report Designer. The .rdlcextension signifies that it is a report for client or local rendering.

4. Make sure the report is selected. Open the Toolbox. From the Toolbox, drag a Table report item onto the report.

5. From the Data Sources window, drag the EmployeeIDfield onto the middle row of the first column in the table. The middle row is the Detail row. Notice that the Header row automatically fills in for you when you specify the Detail row.

6. Drag the StartDatefield onto the Detail row of the second column so that it is next to the EmployeeIDfield.

7. Drag the Amountfield onto the Detail row of the third column so that it is next to the StartDatefield.

You should now have a report that looks like Figure 6-5.

Figure 6-5. Local report in the designer

Now you have a report that uses the dataset you created as a data source. Now add the code shown in Listing 6-3 to the runLocalbutton’s clickevent to populate the dataset and display the values in the ReportViewer control using the .rdlcfile you have created. Make sure the ViewerRVC.csform is open in design view, and double-click the Run Local button. This will create an empty method to handle the button’s clickevent.

Listing 6-3. runLocal clickEvent: Running Report in Local Mode private void runLocal_Click(object sender, EventArgs e) {

Employees empDS = new Employees();

empDS.ReadXml(@"C:\Temp\EmployeePay.xml");

reportViewer.ProcessingMode =

Microsoft.Reporting.WinForms.ProcessingMode.Local;

reportViewer.LocalReport.ReportEmbeddedResource =

"SSRS_Viewer_RVC.EmployeePay.rdlc";

reportViewer.LocalReport.DataSources.Add(

new Microsoft.Reporting.WinForms.ReportDataSource(

"Employees_EmployeePay", empDS.Tables["EmployeePay"]));

reportViewer.RefreshReport();

}

Note

Make sure the name in the data source code matches the name expected by the report, or you will receive an error message when the report runs. You can determine the exact name by opening the .rdlc file using Report Designer and selecting Report ➤Report Data Sources. The dialog box that opens will tell you the name you should use.

Note

The ReportEmbeddedResourcevalue must include the namespace of your project. If you entered spaces for the name of your project when you created it, the namespace will use underscores in place of each space.

Now run the project in debug mode. When the form displays, click Run Local. This ren-ders the local Employee Pay report. At this point, you should see something like Figure 6-6.

You’ve now created a report viewer in a Windows Forms application by using the Web-Browser control and URL rendering and by using the new ReportViewer control. You could stop at this point and just use these methods to render the reports and to display the report parameters, toolbar, and report. However, in this example, you want to use the SSRS 2005 Report Server Web service to get a list of parameters for the selected report and to display them to the user in a Windows Forms application. To get this list of parameters, you need to

eport Server Web service.

Figure 6-6. SSRS Viewer RVC running a locally rendered report

Dans le document Pro SQL Server 2005Reporting Services (Page 182-186)