Browse DevX
Sign up for e-mail newsletters from DevX


Building Report-enabled Applications with the New ReportViewer Controls (Part 1 of 2) : Page 5

Reporting is an integral part of every complete application. The Report Viewer controls greatly reduce the development effort required to report-enable .NET applications. Part one of this two-part article shows you how you can leverage the Windows Forms ReportViewer to integrate your Windows Forms .NET applications with Reporting Services 2005.

Designing a Local Report
With the report data source in place, you can proceed to author the actual report. Don't worry if you don't have Reporting Services installed and cannot use the Report Designer. VS.NET 2005 includes a scaled-down version of Report Designer (for the lack of better term, let's call it Local Report Designer) which you can use to design local reports right inside your code project. To do so, just double-click on the report definition file that you've already added to your project. You will undoubtedly find the Local Report Designer similar to its bigger cousin (the RS Report Designer) but its features are more limited. For example, the Local Report Designer doesn't have Data and Preview tabs because a local report doesn't know where to get its data from. Remember, in local mode, the application supplies data and parameters at runtime.

Handling Images
You may need additional tweaks to migrate a server report to a local report. For example, if the server report uses an external image located in the same folder of the report catalog, you may need to re-adjust the image path. As it stands, the Report Viewer won't attempt to load the image from the current application directory by default. As a workaround, you should use the file:// protocol to load external images. Embedding the image path in the report definition is something you will probably want to avoid. For this reason, the Customer Orders.rdlc report contains a simple VB.NET embedded function which returns the application path.

   Public Function GetAppPath() As String
      Return System.IO.Directory.GetCurrentDirectory
   End Function
Using this function, here's how you can set up the Value property of the logo image to point to the external AWC.jpg file.

   =String.Format("file:///{0}\{1}", Code.GetAppPath(), 
You won't see the image at design time but it should appear when the report is run. Of course, another approach to get away the external images limitation in local mode is to convert all external images to embedded images that are saved in the report definition.

When you are done authoring the report, you can remove the report definition file from the project. Upon deployment, the application setup program could copy the report definition to a known location where the Report Viewer can find it.

Here's a useful tip: To automate copying the local report definition to the application runtime folder at design time, you can define a post-build event in the Build Events tab of the project properties.

Requesting a Local Report
The final step to implement local report processing is writing some code to request the report using the LocalReport object.

   private void RunLocal()
      reportViewer.ProcessingMode = ProcessingMode.Local;
      reportViewer.LocalReport.ReportPath = 
         "Customer Orders.rdlc";
      // Enable hyperlinks to handle clicking on the 
      // Customer ID
      reportViewer.LocalReport.EnableHyperlinks = true;
      // Required since the logo image in Customer Orders 
      // report is external
      reportViewer.LocalReport.EnableExternalImages = true;
      // The application is responsible for collecting 
      // parameters
      ReportParameterInfoCollection parameterInfo =   
      if (parameterInfo.Count > 0)
         ParameterDialog dialog = new 
         DialogResult result = dialog.ShowDialog();
         if (result == DialogResult.Cancel) return;
         // Add report parameters
         // Add report data sources
First, the code configures the ReportViewer for local processing. Next, the code sets LocalReport.ReportPath to the local report definition file (by default, it will try to find it from the executable folder). As a security measure, if the local report uses navigation actions (hyperlinks), you need to explicitly enable hyperlinks (more on this in a moment). Similarly, since the local report loads the AdventureWorks logo as an external image, you need to enable external images. In addition, you'll need to elevate the CAS permissions of the local report by running the report in the current application domain. That's because the GetAppPath() embedded function needs FileIOPermission to get the application path where the external image is located.

Next, you need to take care of the report parameters (if any). The WinReporter launches the ParameterDialog form to collect the report parameters. Next, the code passes the parameters to the local report. Finally, the code loads the application dataset and passes it to the report. The RefreshReport method renders the report locally.

Thanks for your registration, follow us on our social networks to keep up-to-date