We help IT Professionals succeed at work.

Loop through string?

jay-are
jay-are asked
on
Hello Experts:

I have a string value that looks like this:  Demo1,Demo2,Demo3
There can be just 1 value with no commas all the way up to nine values all separated by commas.

I'd like to use these values for parameters and pass them using a reportviewer.  How do I convert the values Demo1,Demo2,Demo3 etc into some sort of parameter list and pass it to the reportviewer?

The attached code gives me the error:  Unable to cast object of type 'System.String' to type 'Microsoft.Reporting.WebForms.ReportParameter'.

'cid is the string value: Demo1,Demo2,Demo3 etc.
            Dim aryCID() = cid.Split(",")
            Dim params As ReportParameter
            For i As Integer = 0 To aryCID.Length - 1
                params = aryCID(i)
            Next
            ReportViewer1.ServerReport.SetParameters(params)

Open in new window

Comment
Watch Question

Try something on these lines:
'cid is the string value: Demo1,Demo2,Demo3 etc.
            Dim aryCID() = cid.Split(",")
            Dim params(aryCID.Length) As ReportParameter
            For i As Integer = 0 To aryCID.Length - 1
                params(i) = New ReportParameter("Param" & i, aryCID(i), True)
            Next
            ReportViewer1.ServerReport.SetParameters(params)

Open in new window

Author

Commented:
Overload resolution failed because no accessible "New" can be called without a narrowing conversion
Argument matching parameter 'values' narrows from 'object' to '1-dimensional array of string'
Argument matching parameter 'values' narros from 'object' to 'string'

That's the error I get for:   params(i) = New ReportParameter("Param" & i, aryCID(i), True)
Try using Convert.ToString() for that
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Change:

    Dim aryCID() = cid.Split(",")

To:

    Dim aryCID() As String = cid.Split(",")

You could also try something like this:

    Dim params As New List(Of ReportParameter)
    For Each param As String In cid.Split(",")
        params.Add(New ReportParameter("Param" & (params.Count + 1), param, True))
    Next
    ReportViewer1.ServerReport.SetParameters(params.ToArray)

Author

Commented:
I get an error now saying ParamName0 doesn't exist.  The paramname is actually CID.  It's a multi-value parameter.  Does that change how I set the parameters in reportviewer?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Which could are you using now?...

Author

Commented:
This, although your code produces the same results.
'cid is the string value: Demo1,Demo2,Demo3 etc.
            Dim aryCID() As String = cid.Split(",")
            Dim params(aryCID.Length) As ReportParameter
            For i As Integer = 0 To aryCID.Length - 1
                params(i) = New ReportParameter("CID" & i, aryCID(i), True)
            Next
            ReportViewer1.ServerReport.SetParameters(params)

Open in new window

Most Valuable Expert 2011
Top Expert 2015

Commented:
I don't think the issue is with the code you posted, per se, but inside the SetParameters() method. Actually, I'm not even saying there is a problem there, but there definitely is a discrepancy between how you've named the parameters in the above code and what SetParameters is expecting.

Author

Commented:
The param that the report expects is called "CID".  Did I use the wrong name in the code?  New ReportParameter("CID"...I thought that's where I used the parameters name?

Author

Commented:
Any ideas?
Where is it throwing the exception? On params(i) = New ReportParameter("CID" & i, aryCID(i), True) or ReportViewer1.ServerReport.SetParameters(params)?

Also, the New ReportParameter("CID" & i, aryCID(i), True) line means it's serving up:

New ReportParameter("CID" & 0, aryCID(0), True), if it's expecting CID1, CID2, etc and not CID0,CID1, etc then this could be the issue.

Author

Commented:
Happens on ReportViewer1.ServerReport.SetParameters(params)

"Parameter 'CID0' does not exist on this report"

When I call this report directly from the reportserver (not using reportviewer) I would use a url like this:
"http://mysite/reportserver/reportname&rs:Command=Render&CID=" & cid1 & "&CID=" & cid2
etc.

So the parameter name should always just be "cid".  I tried changing this:

New ReportParameter("CID" & i, aryCID(i), True)
to
New ReportParameter("CID", aryCID(i), True)

but it gives me this error: Parameter 'CID' was specified multiple times



Author

Commented:
I found this code on another site.  It looks like this can be done pretty easily using a datasource but I'm unable to load my values from the string "cid" into a dataset.


Dim paramList As New List(Of ReportParameter) 
Dim paramDept As New ReportParameter("department")

Dim dsDepartments As DataSet = Me._wsOrg.GetDepartments()
For Each drDept As DataRow In dsDepartments.Tables("TB_Departments")
paramDept.Values.Add(drDept("department"))
Next

paramList.Add(paramDept)

Open in new window

Commented:
Finally, this seems to do the trick!

' cid is the string value: Demo1,Demo2,Demo3 etc.
            Dim aryCID() As String = cid.Split(",")
            Dim paramList As New List(Of ReportParameter)()
            Dim param As New ReportParameter("CID")
            ' Create the string array of values to pass
            Dim values As String() = New String() {}
            ' Add a range of elements from an array to the end of the StringCollection.
            param.Values.AddRange(aryCID)
            ' Add the parameter to the list of ReportParameters
            paramList.Add(param)
            ' Pass param to reportviewer
            ReportViewer1.ServerReport.SetParameters(paramList)

Open in new window