Skip to main content Skip to footer

PageReports : Changing SubReport's DataSource at Run Time

Someone has rightly said: The world is dynamic, we have to change all the time. The quote suits in every aspect of life and technology. From small applications to large projects, the static data and information has been swayed by the dynamic needs of the users. Here in this blog, we will discuss one such scenario often asked by our customers of ActiveReports - Changing PageReports' Subreport's Data at Run Time. Since, in PageReports the subreports' properties are not exposed, the connectionstring/datsource cannot be modified directly. Moreover, when an object is typecasted into a Subreport type of PageReport, only the ReportName property is exposed. So is it not possible to change the connectionstring of Subreports (of PageReports) at run time? The answer is NO.. It is POSSIBLE!!! Img_Subrpt

How? - Read Below

You just need to use the Custom Resource Locator that helps to read any resource (images, themes etc) from any location to be used in drillthrough links, subreports, or master reports. You will have to override the GetResource method of Custom Resource Locator which returns ParentUri and Value properties. Please note, that the required datasource of the Subreport would be set in this method and then this Subreport would be assigned as the located resource to the Value property of the GetResource method. Here is the code of the GetResource method used in the sample :


namespace ConnectDT2Report  
{  
    internal sealed class MySubreportLocator : ResourceLocator  
    {  
        private const string UriSchemeMySubreports = "MySubreport:";  
        ///  
        /// Obtain and return the resource.  
        ///  
        ///The information about the resource to be obtained.  
        /// The resource, null if it was not found.  
        public override Resource GetResource(ResourceInfo resourceInfo)  
        {  
            Resource resource;  
            string name = resourceInfo.Name;  

            Uri uri = new Uri(name);  
            if (uri.GetLeftPart(UriPartial.Scheme).StartsWith(UriSchemeMySubreports, true, CultureInfo.InvariantCulture))  
            {  
                PageReport pr = new PageReport(new System.IO.FileInfo(name.Replace(UriSchemeMySubreports,"").Trim()));  
                pr.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";  
                pr.Report.DataSources[0].ConnectionProperties.ConnectString = @"data source=C:\\Users\\stduser\\Documents\\ComponentOne Samples\\ActiveReports 8\\Data\\reels.MDB;provider=Microsoft.Jet.OLEDB.4.0;";  

                System.IO.MemoryStream memory_stream = new System.IO.MemoryStream();  
                using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(memory_stream))  
                {  
                    pr.Save(writer);  
                }  
                memory_stream.Position = 0;  
                resource = new Resource(memory_stream, uri);  
            }  
            else  
            {  

            }  
            return resource;  
        }  
    }  
}  


For complete implementation, please download the following sample: Download Sample

MESCIUS inc.

comments powered by Disqus