报告参数异常错误

ReportParameter Exception Error

您好,我已经开发了一个具有报告功能的网站,并且运行没有任何问题。现在我想开发一个 Windows Form App 和 C# with reporting function。这就是我所做的:

private void print_Load(object sender, EventArgs e)
    {
        rptViewer.Reset();
        DataTable dt = getData("2");
        ReportDataSource rds = new ReportDataSource("DataSet1", dt);
        rptViewer.LocalReport.DataSources.Add(rds);
        rptViewer.LocalReport.ReportPath = @"PAL\Report1.rdlc";
        ReportParameter[] rptParams = new ReportParameter[] {
        new ReportParameter("invoiceId","2")
        };
        rptViewer.LocalReport.SetParameters(rptParams);
        rptViewer.LocalReport.Refresh();
        this.rptViewer.RefreshReport();
    }

还有另一个用于填充 DataTable 的函数:

private DataTable getData(string id)
    {
        string[] dataName = new string[1];
        dataName[0] = "@invoiceId";
        string[] dataValue = new string[1];
        dataValue[0] = id;
        DataTable dt = new DataTable();
        dt = _cls.FillDataTable("procBasket", dataName, dataValue);
        return dt;
    }

但是当我运行这个程序时,VS会抛出这样的错误:
并且似乎 VS 无法找到报告路径并抛出以下异常:

Exception:Thrown: "Could not find a part of the path 'C:\Users\BNS\Documents\Visual Studio 2013\Projects\nickSell\nickSell\bin\Debug\PAL\Report1.rdlc'." (System.IO.DirectoryNotFoundException) A System.IO.DirectoryNotFoundException was thrown: "Could not find a part of the path 'C:\Users\BNS\Documents\Visual Studio 2013\Projects\nickSell\nickSell\bin\Debug\PAL\Report1.rdlc'." Time: 2015/12/27 Sunday 7:53:53 PM Thread:[10268]

我为路径做了一些事情,但错误仍然存​​在:(

string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
string reportPath = Path.Combine(exeFolder, @"PAL\Report1.rdlc");
rptViewer.LocalReport.ReportPath = reportPath;

最后这是我的解决方案树视图

打印有reportview控件的winform路径是这样的:

改用Application.StartUp

改变这个,

string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);

进入

string exeFolder = Application.StartUp;

编辑:根据我们的 chat,问题是报告路径实际上位于解决方案可执行文件的父文件夹中。这就是发生错误的原因。

为了更正错误,只需转到报告文件夹,方法是:

string dir = Directory.GetParent(Application.StartupPath).FullName; 
dir = Directory.GetParent(dir).FullName; //get parent of parent folder
string reportPath = Path.Combine(dir , @"PAL\Report1.rdlc"); //then do this
rptViewer.LocalReport.ReportPath = reportPath;    

这样,报告路径就会指向正确文件夹中的正确文件。解决这个问题的关键是正确设置文件夹