ReportViewer for WPF and Silverlight | ComponentOne
C1ReportViewer Task-Based Help / Loading Documents into C1ReportViewer / Loading Documents from Files on the Server (Silverlight)
In This Topic
    Loading Documents from Files on the Server (Silverlight)
    In This Topic

    A common usage scenario for the C1ReportViewer control is to have a report server (such as C1Report or Microsoft SQL Server Reporting Services) generate reports on a schedule, and deploy them to the file system on the server. Your Silverlight or WPF applications can then get these files from the server and display them to the user with very little overhead.

    This scenario is illustrated in the C1ReportViewerQuickstart sample.

    After adding the C1ReportViewer to your application, you should add a Silverlight-enabled WCF service to the server project. This service will provide the Silverlight client with the list of reports available and with the actual document streams for each report.

    For example, the following is a typical implementation of a report provider Web service:

    Visual Basic
    Copy Code
    <ServiceContract([Namespace] := "")> _
    <AspNetCompatibilityRequirements(RequirementsMode := AspNetCompatibilityRequirementsMode.Allowed)> _
    Public Class ReportingService
          <OperationContract> _
          Public Function GetReportList() As String()
          Dim path__1 = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources")
          Return Directory.GetFiles(path__1, "*.pdf")
          End Function
          <OperationContract>
    Public Function GetReportStream(reportName As String) As Byte()
          ' get file name
          Dim path__1 = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources")
          reportName = Path.Combine(path__1, reportName)
    
          ' load file into stream
          Dim ms = New MemoryStream()
          Dim buff = New Byte(63999) {}
          Using sr = New FileStream(reportName, FileMode.Open)
                While True
                      Dim read As Integer = sr.Read(buff, 0, buff.Length)
                      ms.Write(buff, 0, read)
                      If read = 0 Then
                            Exit While
                      End If
                End While
          End Using
    End Function
    ' return byte stream
    Return ms.ToArray()
    End Class
    

     

    C#
    Copy Code
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode =
             AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReportingService
    {
      [OperationContract]
      public string[] GetReportList()
      {
        var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources");
        return Directory.GetFiles(path, "*.pdf");
      }
    
      [OperationContract]
      public byte[] GetReportStream(string reportName)
      {
        // get file name
          var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources");
          reportName = Path.Combine(path, reportName);
    
          // load file into stream
          var ms = new MemoryStream();
          var buff =new byte[64000];
          using (var sr = new FileStream(reportName, FileMode.Open))
          {
            for (; ; )
            {
              int read = sr.Read(buff, 0, buff.Length);
              ms.Write(buff, 0, read);
              if (read == 0) break;
            }
          }
        }
    
        // return byte stream
        return ms.ToArray();
      }
    }
    

     

    As you can see, the code is very standard. The first method lists the reports available on the server so the Silverlight application can show a list of reports to the user, and the second method returns the byte stream that represents the selected report.

    The client part of the application uses the service as follows:

    Visual Basic
    Copy Code
    Public Sub New()
          InitializeComponent()
    
          ' go get the list of reports available
          Dim svc = New ReportingServiceReference.ReportingServiceClient()
          AddHandler svc.GetReportListCompleted, AddressOf svc_GetReportListCompleted
          svc.GetReportListAsync()
    End Sub
    
    ' populate ComboBox with list of reports available on the server
    Private Sub svc_GetReportListCompleted(sender As Object, e As ReportingServiceReference.GetReportListCompletedEventArgs)
          _cmbReport.Items.Clear()
          For Each file As String In e.Result
                _cmbReport.Items.Add(Path.GetFileNameWithoutExtension(file))
          Next
          _cmbReport.IsEnabled = True
          _cmbReport.SelectedIndex = 0
    End Sub
    
    ' show the report that was selected
    Private Sub ReportType_Click(sender As Object, e As EventArgs)
          ' build report name
          Dim reportName As String = DirectCast(_cmbReport.SelectedItem, String)
          reportName += If(_btnPDF.IsChecked.Value, ".pdf", ".mhtml")
    
          ' go get the stream
          Dim svc = New ReportingServiceReference.ReportingServiceClient()
          AddHandler svc.GetReportStreamCompleted, AddressOf svc_GetReportStreamCompleted
          svc.GetReportStreamAsync(reportName)
    End Sub
    
    ' display the report
    Private Sub svc_GetReportStreamCompleted(sender As Object, e As ReportingServiceReference.GetReportStreamCompletedEventArgs)
          Dim ms = New MemoryStream(e.Result)
          _reportViewer.LoadDocument(ms)
    End Sub
    

     

    C#
    Copy Code
    public MainPage()
    {
      InitializeComponent();
    
      // go get the list of reports available
      var svc = new ReportingServiceReference.ReportingServiceClient();
      svc.GetReportListCompleted += svc_GetReportListCompleted;
      svc.GetReportListAsync();
    }
    
    // populate ComboBox with list of reports available on the server
    void svc_GetReportListCompleted(object sender,
      ReportingServiceReference.GetReportListCompletedEventArgs e)
    {
      _cmbReport.Items.Clear();
      foreach (string file in e.Result)
      {
        _cmbReport.Items.Add(Path.GetFileNameWithoutExtension(file));
      }
      _cmbReport.IsEnabled = true;
      _cmbReport.SelectedIndex = 0;
    }
    
    // show the report that was selected
    void ReportType_Click(object sender, EventArgs e)
    {
      // build report name
      string reportName = (string)_cmbReport.SelectedItem;
        reportName += _btnPDF.IsChecked.Value ? ".pdf" : ".mhtml";
    
        // go get the stream
        var svc = new ReportingServiceReference.ReportingServiceClient();
        svc.GetReportStreamCompleted += svc_GetReportStreamCompleted;
        svc.GetReportStreamAsync(reportName);
      }
    
      // display the report
      void svc_GetReportStreamCompleted(object sender,
        ReportingServiceReference.GetReportStreamCompletedEventArgs e)
      {
        var ms = new MemoryStream(e.Result);
        _reportViewer.LoadDocument(ms);
      }