5

I'm using the following code with which I'm 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 is it wrong?

I've created a report parameter, the Parameters 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 has displayed in the 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

Sule
  • 137
  • 8
MMD MNC
  • 149
  • 1
  • 5
  • 12

6 Answers6

12

Put ReportParameter after the

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

here you write your ReportParameter

8

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);
Jonathan Hiben
  • 533
  • 7
  • 16
3

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);
Community
  • 1
  • 1
pstrjds
  • 16,840
  • 6
  • 52
  • 61
  • @MMDMNC - Did you add some debug lines to verify what path it is pointing to. You are using a relative path in the code here. It will be relative to the working folder of the executing application. I recommend verifying what the actual path it is looking for is. If you have verified what path it is searching, then there might be other issues, but verify that first. – pstrjds Oct 15 '12 at 20:42
  • i'm use _reportViewer.LocalReport.SetParameters(report); error : An error occurred during local report processing. – MMD MNC Oct 15 '12 at 21:08
1

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);
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Romych
  • 11
  • 1
1

You should change from:

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

To:

_reportViewer.LocalReport.SetParameters(report);
Vilmar Oliveira
  • 117
  • 1
  • 7
0

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.

Gwasshoppa
  • 884
  • 12
  • 18