SSRS执行服务ParameterValue变量索引失败
我正在尝试使用SSRS执行服务ParameterValue数组而不定义索引数。微软的例子如下: http://technet.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx '准备报告参数。 Dim参数(2)As ParameterValue
parameters(0) = New ParameterValue()
parameters(0).Name = "EmpID"
parameters(0).Value = "288"
parameters(1) = New ParameterValue()
parameters(1).Name = "ReportMonth"
parameters(1).Value = "6" ' June
parameters(2) = New ParameterValue()
parameters(2).Name = "ReportYear"
parameters(2).Value = "2004"
但我想添加可变数量的对象。我想做这样的事情: '准备报告参数。 Dim parameters()As ParameterValue
parameters(0) = New ParameterValue()
parameters(0).Name = "EmpID"
parameters(0).Value = "288"
parameters(1) = New ParameterValue()
parameters(1).Name = "ReportMonth"
parameters(1).Value = "6" ' June
parameters(2) = New ParameterValue()
parameters(2).Name = "ReportYear"
parameters(2).Value = "2004"
For x As Integer = 0 To MyList.Count - 1
' Start
Dim n As Integer = x + 3 ' Start adding values after the last entry
parametersRdl(n) = New ParameterValue()
parametersRdl(n).Name = "NameFromMyList"
parametersRdl(n).Value = MyList(x)
Next
显然我无法定义数组中索引的数量,因为我不知道MyList是多长。当我删除索引的数量时,我收到此错误: “NullReferenceException未被用户代码处理。对象引用未设置为对象的实例。“有没有人有SSRS ParameterValue对象的经验?或者我的阵列构建有问题吗?希望我得到一个适用于ParameterValue的答案。
感谢任何帮助,谢谢!
答案 0 :(得分:1)
您正在获取空引用,因为您的数组尚未实例化。你必须设置数组的大小。
如果在声明数组时可以访问MyList对象,则可以执行以下操作来创建列表大小加3的数组。
' Gives size of three + list count
Dim parameters(MyList.Count + 2) As ParameterValue
如果您以后只能访问该列表,可以使用调整大小功能
Array.Resize(parameters, parameters.Length + MyList.Count)
答案 1 :(得分:0)
此函数创建SsrsExecutionService.ParameterValue值的参数列表。然后,它将参数Names和ParameterValue()类型的值添加到列表中。在最后,它执行List.ToArray将ParameterValue列表放入SSRS接受的ParameterValue数组中。
这样做的目的是避免创建ParameterValue数组并且必须在数组中定义索引的大小。 稍后您将把此参数数组传递给报告服务器执行服务:rs.SetExecutionParameters(parameters,“en-us”)
这也是如何将MDX参数发送到SSRS的示例。
Public Function ToMDXParamArray(startDate As String, endDate As String, employeeNames As String, gender As String, ethnicity As String) As SsrsExecutionService.ParameterValue()
' The function was passed a comma delited text string of employee names. They need to be split up and added to the parameter array indivudually.
' If this were a SQL query the whole string could be passed to a single parameter.
Dim employeeList() As String = employeeNames.Split(",")
' Create a parameter list to hold SsrsExecutionService.ParameterValue values
Dim paramList As New List(Of SsrsExecutionService.ParameterValue)
' Define a single ParameterValue. In SSRS this has Name, Value, and Label fields.
Dim p As New SsrsExecutionService.ParameterValue()
' Create a new ParameterValue
p = New SsrsExecutionService.ParameterValue()
' Assigne a name and value
p.Name = "StartDate"
p.Value = "[EmploymentDates].[YearMonthDate].[Month].&[" + startDate + "]"
' Add that ParameterValue to the parameter list
paramList.Add(p)
p = New SsrsExecutionService.ParameterValue()
p.Name = "EndDate"
p.Value = "[EmploymentDates].[YearMonthDate].[Month].&[" + endDate + "]"
paramList.Add(p)
p = New SsrsExecutionService.ParameterValue()
p.Name = "Gender"
p.Value = "[" + gender + "]"
paramList.Add(p)
p = New SsrsExecutionService.ParameterValue()
p.Name = "Ethnicity"
p.Value = ethnicity
paramList.Add(p)
' Now add that list of employee names. For Analysis Services/MDX the names have to be added individually. For SQL you pass the entire string to a single parameter.
' This loop of an unknown number of employees in employeeList is exactly why you don't want to create a parametersRdl(50) As SsrsExecutionService.ParameterValue with a defined index size.
For x As Integer = 0 To employeeList.Count - 1
p = New SsrsExecutionService.ParameterValue()
p.Name = "ProvidersProviderLocation"
p.Value = "[Employees].[Employee Store].[Employees].&[" + employeeList(x) + "]"
paramList.Add(p)
Next
' Create the Execution Service Parameter Value object
Dim parametersRdl() As SsrsExecutionService.ParameterValue
' Assigne the parameter list to the SSRS ParameterValue array
parametersRdl = paramList.ToArray()
' Return the ParameterValue
Return parametersRdl
End Function
在弄清楚这一点之前,我不得不把头撞到墙上,我希望这有助于其他人。