从web.config文件获取值到rdlc报告?

时间:2015-04-23 11:59:30

标签: c# sql asp.net asp.net-mvc rdlc

如何从web.config(appsettings)文件中获取值到我的rdlc报告? 我需要在我的rdlc发票中显示多个值,它们来自web.config文件?我该怎么做这个?

<add key="siteName" value="desktopapplications" />
<add key="companyName" value="ProSoftware Limited" />

在哪里可以在rdlc中编写代码以及如何实现上述值?

我知道如何将值从web.config获取到.cs页面

string siteName= WebConfigurationManager.AppSettings["siteName"]

它如何在rdlc中运行?任何帮助谢谢

2 个答案:

答案 0 :(得分:1)

在“报表数据”面板的RDLC设计器中,添加siteName和companyName的参数。在后面的代码中:

Dim myReport As New LocalReport
Dim params() As ReportParameter = New ReportParameter(1) {}
     params(0) = New ReportParameter("siteName", siteName)
     params(1) = New ReportParameter("companyName", companyName)
With myReport
    .ReportPath = ReportPath '"MyReport.rdlc"
    .DataSources.Clear()
    .DataSources.Add(mydatasource)
    .Refresh()
    .SetParameters(params)
End With

答案 1 :(得分:0)

来自Lon Prosser的回答

       using (var lr = new LocalReport())
        {

            var path = Path.Combine(HttpRuntime.AppDomainAppPath, "Report", "MyInvoice.rdlc");
            lr.ReportPath = path;
            var siteName = ConfigurationManager.AppSettings["siteName"];
            var companyName = ConfigurationManager.AppSettings["companyName"];

            var @params = new ReportParameter[2];

            @params[0] = new ReportParameter("siteName", siteName);
            @params[1] = new ReportParameter("companyName",companyName);
            var usersinv = db.Invoices.Where(i => i.Id == invoiceid.Id);
            var invoice = usersinv.SingleOrDefault();
            if (invoice != null)
            {
                var invoiceitems = db.InvoiceItems.Where(i => i.InvoiceId == invoiceid.Id);
                var rd1 = new ReportDataSource("Invoice", usersinv);
                var rd2 = new ReportDataSource("InvoiceItems", invoiceitems.ToList());

                lr.DataSources.Add(rd1);
                lr.DataSources.Add(rd2);
                lr.SetParameters(@params);
            }
            string encoding;
            string fileNameExtention;

            const string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>21cm</PageWidth>" +
            "  <PageHeight>29.7cm</PageHeight>" +
            "  <MarginTop>0.0cm</MarginTop>" +
            "  <MarginLeft>0.0cm</MarginLeft>" +
            "  <MarginRight>0.0cm</MarginRight>" +
            "  <MarginBottom>0.0cm</MarginBottom>" +
            "</DeviceInfo>";

            Microsoft.Reporting.WebForms.Warning[] warnings;
            string[] streams;

            renderedBytes = lr.Render("PDF",
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtention,
                out streams,
                out warnings);


        }
        return File(renderedBytes, mimeType);

现在,PDF发票中的参数将由web.config文件

调用