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})
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.
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.
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.
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})
Remember to click “Mark as Answer” on the post, if it helps you.
Navneet Kumar Mitawa
Member
3 Points
16 Posts
setting report viewer parameters
Apr 14, 2016 03:45 PM|kclarke|LINK
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.
Star
7980 Points
1586 Posts
Re: setting report viewer parameters
Apr 15, 2016 02:10 AM|Weibo Zhang|LINK
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.
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
Member
3 Points
16 Posts
Re: setting report viewer parameters
Apr 15, 2016 03:03 PM|kclarke|LINK
I do use the server reports as these are reports sitting in my SSRS
Star
7980 Points
1586 Posts
Re: setting report viewer parameters
Apr 20, 2016 10:04 AM|Weibo Zhang|LINK
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
Contributor
2923 Points
1417 Posts
Re: setting report viewer parameters
Apr 20, 2016 10:15 AM|navneetmitawa|LINK
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})
Navneet Kumar Mitawa
Member
3 Points
16 Posts
Re: setting report viewer parameters
Apr 20, 2016 03:26 PM|kclarke|LINK
I tried that but it says "value of type reportparameter cannot be converted to reportparameter"
Contributor
2923 Points
1417 Posts
Re: setting report viewer parameters
Apr 21, 2016 02:57 AM|navneetmitawa|LINK
Hi try this...
Navneet Kumar Mitawa
Member
114 Points
26 Posts
Re: setting report viewer parameters
Apr 21, 2016 06:20 AM|bnarayan|LINK
Hi Kclarke,
Below code snippet is working for me:
Banketeshvar Narayan
https://twitter.com/BNarayanSharma
https://mvp.microsoft.com/en-us/PublicProfile/5001938