如何通过字符串名称创建数据集的实例?

时间:2019-01-31 14:59:54

标签: asp.net vb.net dataset

我正在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 :(得分:0)

我基本上有相同的情况,我想将报告(由用户选择)动态加载到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);
            }
        }

}