locked
setting report viewer parameters RRS feed

  • Question

  • User1096838958 posted

    I'm trying to set a parameter on a report im calling in asp.net but I keep getting, can anyone help me with this ? it driving me nuts.

    "System.InvalidCastException: Unable to cast object of type 'Microsoft.Reporting.WinForms.ReportParameter[]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Reporting.WebForms.ReportParameter]'."

    1 is the value I'm trying to load but I get the same error each time.

            Dim Param1 As New ReportParameter
            Param1.Name = "person"
            Param1.Values.Add(1)
            ReportViewer4.ServerReport.SetParameters(New ReportParameter() {Param1})
    

    Thursday, April 14, 2016 3:45 PM

Answers

  • User-219423983 posted

    Hi kclarke,

    I guess maybe the issue is caused by “Param1.Values.Add(1)”. According to below link that’s about the ReportParameter.Values Property could not be directly passed value. You’d better try using “ReportParameter Constructor (String, String)” to instantiates a new ReportParameter with a name and a value. The value should be string, not a numeric value.

    https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.reportparameter.values.aspx

    Best Regards,

    Weibo Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 20, 2016 10:04 AM
  • User1940134294 posted

    Hi Kclarke,

    Below code snippet is working for me:

      Rpt.ServerReport.ReportServerUrl = new Uri("....");
      Rpt.ServerReport.ReportPath = "......";
      ReportParameter[] parameters = new ReportParameter[5];
      parameters[0] = new ReportParameter("param1",  param1);
      parameters[1] = new ReportParameter("param2",  param2);
      parameters[2] = new ReportParameter("param3",  param3);
      parameters[3] = new ReportParameter("param4",  param4);
      parameters[4] = new ReportParameter("param5",  param5);
      Rpt.ServerReport.SetParameters(parameters);
      Rpt.ServerReport.Refresh();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2016 6:20 AM

All replies

  • User-219423983 posted

    Hi kclarke,

    If you want to pass custom parameters to ReportViewer with RDLC, you’d better use below code pass the parameter, instead of “ReportViewer4.ServerReport” which is for reports sitting at SQL.

    ReportViewer4.LocalReport.SetParameters(New ReportParameter() {Param1})

    For more things, you could have a look at below links that provide related information for this situation.

    http://forums.asp.net/post/5175192.aspx

    http://cybarlab.com/pass-parameter-in-rdlc-report

    About the error message, as I searched shows that the “SetParameters()” accepts an array of ReportParameter objects, not a single object.  It asks us to create an array that contains one element and set that element to your instance of ReportParameter. But, your above code seems right, you'd better double check where causes this error and whether this error is caused by above code.

    Best Regards,

    Weibo Zhang

    Friday, April 15, 2016 2:10 AM
  • User1096838958 posted

    I do use the server reports as these are reports sitting in my SSRS

    Friday, April 15, 2016 3:03 PM
  • User-219423983 posted

    Hi kclarke,

    I guess maybe the issue is caused by “Param1.Values.Add(1)”. According to below link that’s about the ReportParameter.Values Property could not be directly passed value. You’d better try using “ReportParameter Constructor (String, String)” to instantiates a new ReportParameter with a name and a value. The value should be string, not a numeric value.

    https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.reportparameter.values.aspx

    Best Regards,

    Weibo Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 20, 2016 10:04 AM
  • User-1768369891 posted

    Dim Param1 As New ReportParameter Param1.Name = "person" Param1.Values.Add(1) ReportViewer4.ServerReport.SetParameters(New ReportParameter() {Param1})

    I think you use wrong report parameter. you used window form type report parameter .

    Try to web form Report Parameter.  

    Dim Param1 As New ReportParameter
            Param1.Name = "person"
            Param1.Values.Add(1)
            ReportViewer4.ServerReport.SetParameters(New Microsoft.Reporting.WebForms.ReportParameter() {Param1})
    Wednesday, April 20, 2016 10:15 AM
  • User1096838958 posted

    I tried that but it says "value of type reportparameter cannot be converted to reportparameter"

    Wednesday, April 20, 2016 3:26 PM
  • User-1768369891 posted

    Hi try this... 

    Dim Param1 As New Microsoft.Reporting.WebForms.ReportParameter("person","1")
            
    ReportViewer4.ServerReport.SetParameters(New Microsoft.Reporting.WebForms.ReportParameter[] {Param1})
    Thursday, April 21, 2016 2:57 AM
  • User1940134294 posted

    Hi Kclarke,

    Below code snippet is working for me:

      Rpt.ServerReport.ReportServerUrl = new Uri("....");
      Rpt.ServerReport.ReportPath = "......";
      ReportParameter[] parameters = new ReportParameter[5];
      parameters[0] = new ReportParameter("param1",  param1);
      parameters[1] = new ReportParameter("param2",  param2);
      parameters[2] = new ReportParameter("param3",  param3);
      parameters[3] = new ReportParameter("param4",  param4);
      parameters[4] = new ReportParameter("param5",  param5);
      Rpt.ServerReport.SetParameters(parameters);
      Rpt.ServerReport.Refresh();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2016 6:20 AM