Search code examples
c#winformsreportviewer

ReportParameter Exception Error


Hi I already developed a website with reporting function and it working without any problem. Now I want develop a Windows Form App and C# with reporting function. This is what I did:

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();
    }

And another function for filling 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;
    }

But When I run the program, VS throw an error as like this:
enter image description here and It seems VS can't found report path and throw below exception:

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]

I do something for path but error still is exists :(

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

at the end this is my solution tree view
enter image description here
and print winform path that has reportview control is like this:
enter image description here


Solution

  • Try Application.StartUp instead

    Change this,

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

    into

    string exeFolder = Application.StartUp;
    

    Edit: according to our chat, the problem is that the report path is actually located in the parent of parent folder of the solution executable. That is why the error occurs.

    In order to correct the error, simply go to the report folder by doing:

    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;    
    

    This way, the report path points to the correct file in the correct folder. The key to solve this issue is to get the folder right.