The source of the report definition has not been s

2019-02-28 05:45发布

问题:

I'm using the following code trying to SetParametr :

    var report = new ReportParameter[1];
    report[0] = new ReportParameter("MyName", "Raha");
    var reportDataSource1 = new ReportDataSource { Name = "WpfApplication17_User", Value = _users };


    _reportViewer.LocalReport.DataSources.Add(reportDataSource1);
    _reportViewer.ServerReport.SetParameters(report);
    _reportViewer.LocalReport.ReportPath = "../../Report1.rdlc";

    _reportViewer.RefreshReport();

error : The source of the report definition has not been specified

Why wrong?

I've created a report parameter , Parameter name is 'MyName'

UPDATE :

I'm using the following code :

    //var report = new ReportParameter[1];
    //report[0] = new ReportParameter("MyName", "Raha");


    var reportDataSource1 = new ReportDataSource { Name = "WpfApplication17_User", Value = _users };

    string exeFolder = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

    _reportViewer.LocalReport.ReportPath =exeFolder + @"\Reports\Report1.rdlc";


    _reportViewer.LocalReport.DataSources.Add(reportDataSource1);

    //_reportViewer.ServerReport.SetParameters(report);

    _reportViewer.RefreshReport();

data displayed in Report.

but , I'm using the following code :

   var report = new ReportParameter[1];
            report[0] = new ReportParameter("MyName", "Raha");


            var reportDataSource1 = new ReportDataSource { Name = "WpfApplication17_User", Value = _users };

            string exeFolder = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

            _reportViewer.LocalReport.ReportPath = exeFolder + @"\Reports\Report1.rdlc";


            _reportViewer.LocalReport.DataSources.Add(reportDataSource1);

            _reportViewer.ServerReport.SetParameters(report);//error

            _reportViewer.RefreshReport();

error as : The source of the report definition has not been specified

回答1:

Put ReportParameter after the

_reportViewer.LocalReport.ReportPath = "../../Report1.rdlc";

here you write your ReportParameter



回答2:

The error is not related to your parameter. The error is related to the ReportPath. See this other SO question regarding setting the report path. Are you sure the relative path you have specified is correct when your application is running. Is that path relative to your source code? If you want to see where it is looking for the file then you can add some code to resolve that relative path (Path.GetFullPath) and see where it is pointing. Make sure your rdlc file is in that folder.

Edit:
Based on your updated question and verifying that the report is actually being found. I looked in more detail at your code. You are setting the parameters for the ServerReport, but you are loading the report in the LocalReport. Trying setting the parameters in the LocalReport.

_reportViewer.LocalReport.SetParameters(report);


回答3:

I found myself stuck with the same error, and the reason is actually quite simple: the Report Definition must be set in the first place, before the parameters. This way, the LocalReport can check if they are really mandatory and eventually throw an exception.

The code below should work:

_reportViewer.LocalReport.DataSources.Add(reportDataSource1);
_reportViewer.LocalReport.ReportPath = "../../Report1.rdlc";
_reportViewer.ServerReport.SetParameters(report);


回答4:

I had the same problem today, in my case the reason "copy-paste" issue. Just replacing ServerReport with LocalReport in the code below solved the problem. Change

_reportViewer.LocalReport.DataSources.Add(reportDataSource1);

to

_reportViewer.ServerReport.SetParameters(report);


回答5:

You should change from:

_reportViewer.ServerReport.SetParameters(report); //error

To:

_reportViewer.LocalReport.SetParameters(report);


回答6:

It is possible that your report is an Embedded Resource and thus when you try to set a parameter with _reportViewer.ServerReport.SetParameters(report);//error your report definition has not yet been loaded.

Therefore if your report is an Embedded Resource then you need to call report.LoadReportDefinition(stream); // Get report definition before you set your report parameters.

i.e: The below returns a PDF byte array from a LocalReport

public byte[] ProcessReportToPDFBytes(List<ReportDataSource> rds, Stream stream, string fileName, string outputType, SqlParameter[] rptParameters)
    {
      // Variables 
      Warning[] warnings;
      string[] streamIds;
      string mimeType = string.Empty;
      string encoding = string.Empty;
      string extension = string.Empty;

    using (LocalReport report = new LocalReport())
    {
            // Setup the report viewer object and get the array of bytes 
            report.EnableHyperlinks = true;
            report.EnableExternalImages = true;
            report.SetBasePermissionsForSandboxAppDomain(new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted));

            report.LoadReportDefinition(stream); // Get report definition
            // **** Set the Report Parameters AFTER the LoadReportDefinition ****
            if (rptParameters != null)
            {
              foreach (SqlParameter param in rptParameters)
              {
                report.SetParameters(new ReportParameter(param.ParameterName, param.Value == null ? "" : param.Value.ToString(), false));
              }
            }
            foreach (ReportDataSource rds1 in rds)
            {
              report.DataSources.Add(rds1); // Add datasource here 
            }

            // Render the PDF from the local report
            return report.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
    }
}

Hope this may help others with this similar error.