Share via


Lesson 3: Load a Report Definition from the Report Server

After you have created your project and generated the classes from the RDL schema, you are ready to load a report definition from the report server.

To load a report definition

  1. Add a private field at the top of the ReportUpdater class (module if you are using Visual Basic) for the Report class. This field will be used to maintain a reference to report that is loaded from the report server for the life of the application.

    private Report _report;
    
    Private m_report As Report
    
  2. Replace the code for the LoadReportDefinition() method in the Program.cs file (Module1.vb for Visual Basic) with the following code:

    private void LoadReportDefinition()
    {
        System.Console.WriteLine("Loading Report Definition");
    
        string reportPath = 
            "/AdventureWorks Sample Reports/Company Sales";
    
        // Retrieve the report defintion 
        // from the report server
        byte[] bytes = 
            _reportService.GetReportDefinition(reportPath);
    
        if (bytes != null)
        {
            XmlSerializer serializer = 
                new XmlSerializer(typeof(Report));
    
            // Load the report bytes into a memory stream
            using (MemoryStream stream = new MemoryStream(bytes))
            {
                // Deserialize the report stream to an 
                // instance of the Report class
                _report = (Report)serializer.Deserialize(stream);
            }
        }
    }
    
    Private Sub LoadReportDefinition()
    
        System.Console.WriteLine("Loading Report Definition")
    
        Dim reportPath As String = _
            "/AdventureWorks Sample Reports/Company Sales"
    
        'Retrieve the report defintion 
        'from the report server
        Dim bytes As Byte() = _
            m_reportService.GetReportDefinition(reportPath)
    
        If Not (bytes Is Nothing) Then
    
            Dim serializer As XmlSerializer = _
                New XmlSerializer(GetType(Report))
    
            'Load the report bytes into a memory stream
            Using stream As MemoryStream = New MemoryStream(bytes)
    
                'Deserialize the report stream to an 
                'instance of the Report class
                m_report = CType(serializer.Deserialize(stream), _
                                 Report)
    
            End Using
    
        End If
    
    End Sub
    

Next Lesson

In the next lesson, you will write code to update the report definition that was loaded from the report server. See Lesson 4: Update the Report Definition Programmatically.