Search code examples
c#reporting-servicesreportviewerrdlclocalreport

Generating report from rdlc xml string


I'm facing an issue when generating reports.

I should generate report dynamically using all data from database for it (source name and rdlc report that stores as xml string)

So I solved problem next way:

  1. Create local rdlc file with report xml string;
  2. Set path to the file;

    var rViewer = new ReportViewer();
    rViewer.LocalReport.ReportPath = @"Path to the generated xml rdlc file";
    var dataSource = new ReportDataSource("DataSourceName", data);
    rViewer.LocalReport.DataSources.Add(dataSource);
    

Could I specify in the code-behind directly Report XML String instead of generating the rdlc file and set path to it? Could you recommend a better way?

Thank You


Solution

  • Yes, You can provide it a StringReader object using LoadReportDefinition like so,

    private void Page_Load(object sender, System.EventArgs e)
    {
        NMBS.ViewModels.ViewModelReport Report = (Session["CurrentReport"] as NMBS.ViewModels.ViewModelReport);
    ReportViewer.ProcessingMode = ProcessingMode.Local;
    ReportViewer.PageCountMode = PageCountMode.Actual;
    
    // Set report specifics.
    ReportViewer.LocalReport.DisplayName = Report.Name;
    ReportViewer.LocalReport.LoadReportDefinition(new System.IO.StringReader(Report.Definition));
    foreach (ReportParameter Param in Report.Parameters)
    {
        ReportViewer.LocalReport.SetParameters(Param);
    }
    foreach (ReportDataSource Rds in Report.Data)
    {
        //Load Report Data.
        ReportViewer.LocalReport.DataSources.Add(Rds);
    }
    ReportViewer.CurrentPage = Report.CurrentPage;
    ReportViewer.Width = Report.Width;
    
    // Refresh the reports.
    ReportViewer.LocalReport.Refresh();
    }
    

    Where Report.Defintion is the String containing the XML of the Report's Definition.