Как создать экземпляр DataSet по имени строки? - PullRequest
0 голосов
/ 31 января 2019

Я создаю средство динамического просмотра отчетов в ASP.Net (VB.Net), которое динамически загружает RDLC, и все работает так, как они должны.Единственная проблема, с которой я сталкиваюсь, - это когда я пытаюсь создать экземпляр DataSet, используя его строковое имя, чтобы соответственно заполнить данные отчета.

Ниже приведен код, который я пытался запустить без удачи:

    Dim MyInstance As Object = Activator.CreateInstance(Type.GetType("ClassList"))
    MyInstance = GetReportData()
    Dim datasource As New ReportDataSource(FID, MyInstance.Tables(FID))
    rptViewer.LocalReport.DataSources.Clear()
    rptViewer.LocalReport.DataSources.Add(datasource)

GetType всегда возвращает Nothing и, таким образом, CreateInstance выдает ошибку.Ниже приведен код, который отлично работает:

    Dim MyInstance As Object = Activator.CreateInstance(GetType(ClassList))
    MyInstance = GetReportData()
    Dim datasource As New ReportDataSource(FID, MyInstance.Tables(FID))
    rptViewer.LocalReport.DataSources.Clear()
    rptViewer.LocalReport.DataSources.Add(datasource)

Заранее спасибо,

1 Ответ

0 голосов
/ 31 января 2019

У меня был такой же сценарий, когда я хочу динамически загружать отчет (выбранный пользователем) в SSRS Web Viewer.Я делаю это по соглашению.Все знают, как использовать набор данных по умолчанию DataSet1, DataSet2, 3, 4, ... Кроме того ... я не знаю, как запросить отчет RDLC, как называется данное имя источника данных?Я предполагаю, что RDLC - это всего лишь XML, и вы можете разобрать его, чтобы узнать.Но независимо, я просто иду по простому соглашению.

class DynamicLoadingRDLC
{
    protected static System.Data.DataSet rptds = null;
    private void loadRDLReport(UDParamters pprms, ICustomRDL rdl)
    {

        this.ButtonDownloadReport.Enable();
        this.PanelMain.AutoScroll = false;

        if (rdl == null)
            throw new Exception("Unable to load report!");


        if (rdl == null)
            throw new Exception("Unable to cast report!");

        // returns a dataset from the selected report
        var ds = rdl.ReportData(me.Clinician, Context, pprms);

        // set the dataset into the static class ... we need this if this report has a subreport
        rptds = ds;

        // register the callback for subreports
        ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SetSubDataSource);

        // reset the viewer
        this.ReportViewer1.Reset();



        /*
        You can get more then 1 table added to a report.  Tables will be ordered by name.  Each will be added as DataSet1,2,3,4...
        Code your RDLC and XSD accordingly...
        */
        for (int i = 0; i < rptds.Count(); i++)
        {
                Microsoft.Reporting.WebForms.ReportDataSource src = new Microsoft.Reporting.WebForms.ReportDataSource("DataSet" + (i + 1), rptds.Tables[i]);
                e.DataSources.Add(src);
        }

        this.ReportViewer1.LocalReport.ReportPath = "custom/" + rdl.RDLFile();
        this.ReportViewer1.LocalReport.Refresh();
    }

        public void SetSubDataSource(object sender, SubreportProcessingEventArgs e)
        {
            if (rptds == null)
                return;



            for (int i = 0; i < rptds.Count(); i++)
            {
                Microsoft.Reporting.WebForms.ReportDataSource src = new Microsoft.Reporting.WebForms.ReportDataSource("DataSet" + (i + 1), rptds.Tables[i]);
                e.DataSources.Add(src);
            }
        }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...