Articles in this section

How to Change a Report Query Dynamically in Bold Reports ASP.NET Core

Published:
Updated:

To change a report’s dataset query dynamically at runtime in Bold Reports, use the ReportSerializer class to deserialize the RDL report definition, update each dataset’s Query.CommandText, then pass the modified definition back to the Report Viewer or Report Writer.

Steps

The technique is the same for both components: open the RDL, deserialize it, rewrite each dataset’s CommandText, then hand the modified definition back. Replace the example query below with your own SELECT statement.

Change the query in the Report Viewer

  1. Add the OnInitReportOptions method to your report controller. The Report Viewer calls it while initializing the report.
  2. Open the report file, deserialize it, update each dataset’s CommandText, and assign the modified definition back to reportOption.ReportModel.ReportDefinition.
[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    // Get the base path of the web root.
    string basePath = _hostingEnvironment.WebRootPath;
    // Open the report file stream.
    FileStream reportStream = new FileStream(
        Path.Combine(basePath, "Resources", reportOption.ReportModel.ReportPath),
        FileMode.Open, FileAccess.Read);
    // Deserialize the report definition.
    ReportSerializer serializer = new ReportSerializer();
    ReportDefinition reportDefinition = serializer.GetReportDefinition(reportStream);
    // Modify the query text for each dataset.
    foreach (var dataSet in reportDefinition.DataSets)
    {
        // Replace this with your own SELECT statement.
        dataSet.Query.CommandText = "SELECT ProductID, Name, ListPrice FROM Production.Product WHERE ListPrice > 0";
    }
    // Set the modified report definition in the report model.
    reportOption.ReportModel.ReportDefinition = reportDefinition;
}

Change the query in the Report Writer

  1. In your export action, load the RDL file and deserialize it with ReportSerializer.
  2. Update each dataset’s CommandText, then load the modified definition into ReportWriter with LoadReport and export it to the requested format.
```csharp
[HttpPost]
public IActionResult Export(string writerFormat)
{
    // Load the sample report file.
    FileStream inputStream = new FileStream(
        Path.Combine(_hostingEnvironment.WebRootPath, "Resources", "Test.rdl"),
        FileMode.Open, FileAccess.Read);
    // Create a ReportWriter instance.
    ReportWriter writer = new ReportWriter();
    string fileName = null;
    WriterFormat format;
    string type = null;
    // Deserialize the report definition.
    ReportSerializer serializer = new ReportSerializer();
    ReportDefinition reportDefinition = serializer.GetReportDefinition(inputStream);
    // Modify the query text for each dataset.
    foreach (var dataSet in reportDefinition.DataSets)
    {
        // Replace this with your own SELECT statement.
        dataSet.Query.CommandText = "SELECT ProductID, Name, ListPrice FROM Production.Product WHERE ListPrice > 0";
    }
    // Determine the format and file name based on the writerFormat.
    if (writerFormat == "PDF")
    {
        fileName = "Test.pdf";
        type = "pdf";
        format = WriterFormat.PDF;
    }
    else if (writerFormat == "Word")
    {
        fileName = "Test.docx";
        type = "docx";
        format = WriterFormat.Word;
    }
    else if (writerFormat == "CSV")
    {
        fileName = "Test.csv";
        type = "csv";
        format = WriterFormat.CSV;
    }
    else
    {
        fileName = "Test.xlsx";
        type = "xlsx";
        format = WriterFormat.Excel;
    }
    // Load the report definition.
    writer.LoadReport(reportDefinition);
    // Create a memory stream to hold the exported report.
    MemoryStream memoryStream = new MemoryStream();
    // Save the report to the memory stream.
    writer.Save(memoryStream, format);
    // Set the memory stream position to the beginning.
    memoryStream.Position = 0;
    // Return the generated export document to the client side.
    FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + type);
    fileStreamResult.FileDownloadName = fileName;
    return fileStreamResult;
}

See also

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied