Flylib.com

Books Software

 
 
 

Section 8.2. Report Viewer Control


8.2. Report Viewer Control

The report viewer control is a freely distributable control that lets you embed Reporting Services functionality into a .NET application. The control is called ReportViewer , ships with Visual Studio 2005, and is located in the Data section of the Toolbox in the Visual Studio 2005 IDE.

Follow these steps to build an example that uses the report viewer control.

  1. Launch Visual Studio 2005.

  2. Create a new C# Windows application . Select File New Project from the main menu. Select Visual C# Windows project type, and the Windows Application template. Name the application SS2005RE ReportViewerControl . Press OK to create the application.

  3. Open the form and give it a meaningful caption . Right-click Form1.cs in the Solution Explorer pane and select Open from the context menu. Set its Text property = Report Viewer Control Example .

  4. Drag the ReportViewer control from the Toolbox onto Form1 . The ReportViewer control menu prompts you to either Choose Report from a drop-down list or Design a new report . Click Design a new report to bring up Report Designer. Select <Server Report> , and you are prompted for the Report Server Url and the Report Path that together specify an existing report.

    Set Report Server Url = http://localhost/reportserver and Report Path = /AdventureWorks Sample Reports/Company Sales .

  5. Make the form a bit larger so that the Report Viewer control fits within it.

  6. Run the applicationthe resulting output is shown in the following figure.

You can control and change the report at runtime by modifying the properties of the control's ServerReport objectexposed through the ServerReport property.



8.3. Web Services

Reporting Services provides full access to report server functionality through web services. The web service provides methods and properties for both report executioncontrolling the processing and rendering of reportsand report management.

The example in this section accesses a report from a .NET applicationyou can use any development tool that is capable of invoking SOAP methods.

This example builds a Windows application that retrieves the list of reports in the Adventure Works Sample Reports folder and displays them in a list box. The user can render a report and save it to a Web archive (.mht) file. The complete, running application is shown in the following figure.

Figure 8-2. Completed Application (Running)

Follow these steps to build and run the example.

  1. Launch Visual Studio 2005.

  2. Create a new C# Windows application . Select File New Project from the main menu. Select Visual C# Windows project type, and the Windows Application template. Name the application SS2005RE WebService. Press OK to create the application.

  3. Open the form and give it a meaningful caption . Right-click Form1.cs in the Solution Explorer pane and select Open from the context menu. Set its Text property = AdventureWorks Sample Reports .

  4. Add a list of reports and a button to render the report selected in the list . Drop a ListBox control onto Form1 and name it reportListBox . Drop a Button control onto Form1 , set its Name property = renderButton , and set its Text property = Render.

    The form is shown in the following figure.

    Figure 8-3. Completed Form1 Design

  5. Right-click on References in the Solution Explorer pane and select Add Web Reference... from the context menu to display the Add Web ReferenceStart Browsing for Web Services dialog as shown in the following figure.

    Figure 8-4. Add Web Reference Dialog

  6. Click on the Web services on the local machine link to display a dialog listing Web Services on the local machine as shown in the following figure.

    The ReportService2005 Web service is used to programmatically manage reporting objects. The ReportExecution2005 Web service is used to programmatically process and render reports.

  7. Click the ReportService2005 link to display a dialog describing the Web service.

  8. In the "ReportingService2005" Description dialog, set the Web reference name = RS2005 as shown in the completed dialog shown in the following figure.

    Figure 8-5. ReportingService2005 Description Dialog

    Click the Add Reference button to add the Web service reference to the project. The Web References node in the Solution Explorer pane will update to show the new Web service reference.

  9. Follow steps 5 through 8 to add reference to the ReportExecution2005 servicename the reference RE2005 .

  10. Open the code for Form1 . Right-click Form1.cs in the Solution Explorer pane and select View Code from the context menu.

  11. Add using directives to the top of the class for the ReportService2005 and ReportExecution2005 Web services:

    using SS2005RE_WebService.RE2005;
    using SS2005RE_WebService.RS2005;
    

    Add a using directive to the top of the class for IO:

    using System.IO;
    

  12. Add an event handler to populate the list box when the form is created . Switch to the form designer view. Double-click on the form (but not on the list box or button) to add an event handler for the Load event. Switch back to the code view and add the following code for the Form1_Load method:

    // Instantiate Web service proxy used to programmatically
    // report objects
    ReportingService2005 rs = new ReportingService2005();
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    
    // Get reports in the folder
    CatalogItem[] cis = rs.ListChildren(@"/AdventureWorks Sample Reports",
          false);
    foreach (CatalogItem ci in cis)
    {
          // Populate list box with Report items
          if (ci.Type == ItemTypeEnum.Report)
                reportListBox.Items.Add(ci.Name);
    }
    

  13. Add an event handler to render and save the report when the Render button is clicked . Switch to the form designer view. Double-click on the button renderButton to add an event handler for the load event. Add code to the renderButton_Click method to render the report to a user-specified file.

    The complete code for Form1.cs , including the renderButton_Click event handling code, follows :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    using System.IO;
    
    using SS2005RE_WebService.RE2005;
    using SS2005RE_WebService.RS2005;
    
    namespace SS2005RE_WebService
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Instantiate Web service proxy used to programmatically
                // manage reporting objects
                ReportingService2005 rs = new ReportingService2005();
                rs.Credentials =
                      System.Net.CredentialCache.DefaultCredentials;
    
                // Get reports in the folder
                CatalogItem[] cis = rs.ListChildren(
                      @"/AdventureWorks Sample Reports", false);
                foreach (CatalogItem ci in cis)
                {
                    // Populate list box with Report items
                    if (ci.Type == ItemTypeEnum.Report)
                        reportListBox.Items.Add(ci.Name);
                }
            }
    
            private void renderButton_Click(object sender, EventArgs e)
            {
                if (reportListBox.SelectedIndex >= 0)
                {
                    // Get the name of the report selected in the list box
                    string reportName = reportListBox.Items
                      [reportListBox.SelectedIndex].ToString();
    
                    // Get the name to save the report to
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Web archive (*.mht)*.mht";
                    sfd.FileName = reportName;
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string renderFileName = sfd.FileName;
    
                        // Instantiate Web service proxy used to
                        // programmatically process and render reports
                        ReportExecutionService re =
                            new ReportExecutionService();
                        re.Credentials =
                            System.Net.CredentialCache.DefaultCredentials;
    
                        // byte array to hold rendered report
                        byte[] report;
    
                        // report rendering argument
                        string reportFullName =
                           @"/AdventureWorks Sample Reports" + "/" +
                           reportName;
                        string format = "MHTML";
                        string historyID = null;
                        string deviceInfo = null;
                        RE2005.ParameterValue[] pv =
                            new RE2005.ParameterValue[0];
                        string encoding;
                        string mimeType;
                        string extension;
                        RE2005.Warning[] warning = null;
                        string[] streamIDs = null;
    
                        try
                        {
                            // load report, set parameters, and render
                            re.LoadReport(reportFullName, historyID);
                            re.SetExecutionParameters(pv, "en-us");
                            report = re.Render(format, deviceInfo,
                                  out extension, out mimeType,
                                  out encoding, out warning,
                                  out streamIDs);
    
                            // save report to user-specified file
                            using (FileStream fs =
                                  File.OpenWrite(renderFileName))
                                fs.Write(report, 0, report.Length);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
    }
    

  14. Compile and run the report . Select the Company Sales report and click the Render button. Accept the default file name Company Sales . Close the application. Open the file to view the saved report as shown in the following figure.

    Figure 8-6. Saved Company Sales Report Company Sales.mht