【问题标题】:How to create instance of a DataSet by String Name?如何通过字符串名称创建 DataSet 的实例?
【发布时间】:2019-06-25 01:51:38
【问题描述】:

我正在 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)

提前谢谢你,

【问题讨论】:

  • 您能澄清一下什么是字符串名称吗?您是否尝试使用特定列创建 DataSet?
  • “ClassList”是驻留在 App_Code 文件夹中的现有 DataSet (ClassList.xsd) 的名称。

标签: asp.net vb.net dataset


【解决方案1】:

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

}

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2014-09-06
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多