首页 > 解决方案 > 如何通过字符串名称创建 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)

先感谢您,

标签: asp.netvb.netdataset

解决方案


我基本上有相同的场景,我想将报告(由用户选择)动态加载到 SSRS Web 查看器中。我按照惯例这样做。每个人都知道使用默认的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);
            }
        }

}

推荐阅读