Articles in this section
Category / Section

How to Change the Data Source Dynamically using Report Serializer


Using the ReportSerializer class in Bold Reports, we can modify report details at runtime. This functionality allows you to dynamically customize the report’s data source connection with different data sources.

public void OnInitReportOptions(ReportViewerOptions reportOption)
   // Open the report file as a FileStream.
   FileStream inputStream = new FileStream(@"C:\Users\Resources\sales-order-detail.rdl", FileMode.Open, FileAccess.Read);

   // Create an instance of the ReportSerializer class.
   BoldReports.RDL.DOM.ReportSerializer serializer = new BoldReports.RDL.DOM.ReportSerializer();

   // Get the ReportDefinition from the report file using the serializer.
   BoldReports.RDL.DOM.ReportDefinition reportDefinition = serializer.GetReportDefinition(inputStream);

   // Define the connection string for the data source.
   string connectionString = "Data;Initial Catalog=AdventureWorks;User ID='demoreadonly@data-platform-demo';Password='N@c)=Y8s*1&dh'";

   // Iterate through each data source in the report definition.
   foreach (var source in reportDefinition.DataSources)
       // Modify the connection string of each data source.
       source.ConnectionProperties.ConnectString = connectionString;

   // Create a MemoryStream to save the modified report definition.
   MemoryStream reportStream = new MemoryStream();

   // Save the modified report definition to the MemoryStream using the serializer.
   serializer.SaveReportDefinition(reportStream, reportDefinition);

   // Reset the position of the MemoryStream.
   reportStream.Position = 0;

   // Set the modified report stream as the ReportModel's stream in the report options.
   reportOption.ReportModel.Stream = reportStream;

Was this article useful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied