Monday, August 1, 2011

USE CRL To Display Subreports

DataDynamics Reports expects that the RDLX for a Main Report and a SubReport exist in the same physical location on the File System. But many a times, we may need to pull up the SubReport from a different location or probably from a database \ memory. The ResourceLocator Class within the DataDynamics.Reports.Extensibility namespace can be used to implement the same. We would need to :-

1)    Create a new Class inheriting the ResourceLocator class.
2)    Use code to check the ResourceName (the subreport) being looked for and return the valid subreport object through a MemoryStream or something similar.
C#:-

Using DataDynamics.Reports.Extensibility;

Using (var _stream = GetType ().Assembly.GetManifestResourceStream (GetType (), "MainReport.rdlx"))
            {
                using (var _reader = new StreamReader (_stream))
                {
                    var _report = new ReportDefinition (_reader);
                    _report. ResourceLocator = new SubReportResourceLocator();
                    this.reportViewer.OpenReport (_report, "Test");
                   
                }
            }
Class SubReportResourceLocator: ResourceLocator
    {

        Public override Resource GetResource (ResourceInfo resourceInfo)
        {
            Resource _resource;
            if (resourceInfo.Name.Equals("SubReport.rdlx"))
            {
                byte[] _buffer = System.Text.UTF8Encoding.UTF8.GetBytes (File.ReadAllText("../../SubReport.txt"));
              
                MemoryStream _ms = new MemoryStream (_buffer);
               
               
                if (_ms != null)
                {
                    _resource = new Resource (_ms, new Uri ("C :\\"));
                }
            }

            return _resource;
        }


VB.NET:-
Using _stream = [GetType]().Assembly.GetManifestResourceStream([GetType](), "MainReport.rdlx")
            Using _reader = New StreamReader (_stream)
                Dim _report = New ReportDefinition (_reader)
                _report. ResourceLocator = New SubReportResourceLocator ()    Me.ReportViewer.OpenReport (_report, "Test")
            End Using
        End Using

Public Class SubReportResourceLocator
    Inherits ResourceLocator
 Public Overrides Function GetResource (ByVal resourceInfo As DataDynamics.Reports.Extensibility. ResourceInfo) As DataDynamics.Reports.Extensibility. Resource
        Dim _resource As Resource
        If resourceInfo.Name.Equals ("SubReport.rdlx") Then
            Dim _buffer As Byte () = System.Text.UTF8Encoding.UTF8.GetBytes (File.ReadAllText ("../../SubReport.txt"))
         Dim _ms As New MemoryStream (_buffer)
         If _ms IsNot Nothing Then
                _resource = New Resource(_ms, New Uri("C:\"))
            End If
        End If

        Return _resource
    End Function
End Class

No comments:

Post a Comment