none
Get parameters and datasets from RDLC at runtime RRS feed

  • Question

  • We have an ASP.Net webform app (C#) with a ReportViewer control. We need to run the reports without prior knowledge of the parameters and datasets. The user will select an rdlc file from a folder. Is there a way to dynamically get the parameters and datasets without doing a straight xml read of the file? 

    We're going to display text boxes for the parameter values and then execute ADO.Net code to populate DataTables to assign to the ReportViewer at runtime. Is this all possible?

    Thanks in advance,

    Doug


    -DStiers

    • Moved by Bob Shen Thursday, June 20, 2013 4:20 AM
    Wednesday, June 19, 2013 11:13 AM

Answers

  • It can be done as long as you have a way to determine what data to get based on the names of the report's datasources. Here is some mocked up code, it would get more complicated if you need to accommodate sub reports.

    string rptPath = @"c:\somepath";
                Microsoft.Reporting.WinForms.LocalReport rpt = new Microsoft.Reporting.WinForms.LocalReport();
    
                using (System.IO.TextReader rdr = System.IO.File.OpenText(rptPath))
                {
                    rpt.LoadReportDefinition(rdr);
                    Microsoft.Reporting.WinForms.ReportParameterInfoCollection rptParams = rpt.GetParameters();
                    foreach (Microsoft.Reporting.WinForms.ReportParameterInfo paramInfo in rptParams)
                    {
                        //paramInfo.DataType...
                        //paramInfo.Name ...
                        //paramInfo.Values...
                        //... etc.
                    }
    
                    IEnumerable<string> datanames = rpt.GetDataSourceNames();
                    DataTable dt = null;
                    foreach (string dsName in datanames)
                    {
                        //somehow figure out how to get data for that name
                        //switch (dsName)
                        //{
                        //    case "foo":
                        //        ds = getDataset("foo");
                        //        break;
                        //    case "bar":
                        //        ds = getDataSet("bar");
                        //        break;
                        //    default:
                        //        break;
                        //}
    
                        rpt.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(dsName, dt));
                    }
    
                    reportViewer1.LocalReport.ReportPath = rptPath;
                    reportViewer1.RefreshReport();
                }


    Bob - www.crowcoder.com

    • Marked as answer by DStiers Wednesday, June 19, 2013 2:45 PM
    Wednesday, June 19, 2013 12:34 PM

All replies

  • It can be done as long as you have a way to determine what data to get based on the names of the report's datasources. Here is some mocked up code, it would get more complicated if you need to accommodate sub reports.

    string rptPath = @"c:\somepath";
                Microsoft.Reporting.WinForms.LocalReport rpt = new Microsoft.Reporting.WinForms.LocalReport();
    
                using (System.IO.TextReader rdr = System.IO.File.OpenText(rptPath))
                {
                    rpt.LoadReportDefinition(rdr);
                    Microsoft.Reporting.WinForms.ReportParameterInfoCollection rptParams = rpt.GetParameters();
                    foreach (Microsoft.Reporting.WinForms.ReportParameterInfo paramInfo in rptParams)
                    {
                        //paramInfo.DataType...
                        //paramInfo.Name ...
                        //paramInfo.Values...
                        //... etc.
                    }
    
                    IEnumerable<string> datanames = rpt.GetDataSourceNames();
                    DataTable dt = null;
                    foreach (string dsName in datanames)
                    {
                        //somehow figure out how to get data for that name
                        //switch (dsName)
                        //{
                        //    case "foo":
                        //        ds = getDataset("foo");
                        //        break;
                        //    case "bar":
                        //        ds = getDataSet("bar");
                        //        break;
                        //    default:
                        //        break;
                        //}
    
                        rpt.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(dsName, dt));
                    }
    
                    reportViewer1.LocalReport.ReportPath = rptPath;
                    reportViewer1.RefreshReport();
                }


    Bob - www.crowcoder.com

    • Marked as answer by DStiers Wednesday, June 19, 2013 2:45 PM
    Wednesday, June 19, 2013 12:34 PM
  • ASP.Net related question should be asked over in this Forum:

    http://forums.asp.net/

    This Forum is only for Visual C# question in general.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Wednesday, June 19, 2013 1:27 PM
  • I understand but this is really a c# question pertaining to the report viewer control. I couldnt find a more specific forum. 

    -DStiers

    Wednesday, June 19, 2013 2:42 PM
  • Thanks a lot Bob, thats the class I was looking for: LocalReport

    -DStiers

    Wednesday, June 19, 2013 2:46 PM
  • Hi DStiers,

    I would like to redirect you to appropriate forum for better responses.  


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 20, 2013 4:20 AM