Vb.net 如何在reporting services(SSRS)报告中将可空字段传递给自定义函数?
将SSRS 2012与Visual Studio 2012配合使用 我有一个文本框,其表达式定义如下:Vb.net 如何在reporting services(SSRS)报告中将可空字段传递给自定义函数?,vb.net,reporting-services,Vb.net,Reporting Services,将SSRS 2012与Visual Studio 2012配合使用 我有一个文本框,其表达式定义如下: =Microsoft.VisualBasic.Interaction.IIF(First(Fields!Cost.Value, "SomeTable") Is Nothing, "", Code.GetCostAsString(First(Fields!Cost.Value, "SomeTable"))) Function GetCostAsString(val As Decimal) As
=Microsoft.VisualBasic.Interaction.IIF(First(Fields!Cost.Value, "SomeTable") Is Nothing, "", Code.GetCostAsString(First(Fields!Cost.Value, "SomeTable")))
Function GetCostAsString(val As Decimal) As String
Dim res As Decimal = Val * 1.1D
Return res.ToString("0.##")
End Function
“Cost”是可为空的十进制类型。
“GetCostaString”是自定义VB.NET,如下所示:
=Microsoft.VisualBasic.Interaction.IIF(First(Fields!Cost.Value, "SomeTable") Is Nothing, "", Code.GetCostAsString(First(Fields!Cost.Value, "SomeTable")))
Function GetCostAsString(val As Decimal) As String
Dim res As Decimal = Val * 1.1D
Return res.ToString("0.##")
End Function
(实际上,自定义代码将比这更复杂,这就是我想使用自定义函数的原因)
问题:是否可以将更多代码移到函数中,以便该函数也可以处理字段值为null(即VB.NET中的“Nothing”)的情况
e、 g.因此表达式变为:
=Code.GetCostAsString(First(Fields!Cost.Value, "SomeTable"))
其功能是:
Function GetCostAsString(val As ????) As String
' return empty string if val is Nothing, else do other stuff with decimal value.
End Function
我可以在“?”中输入什么类型,以便它可以接受一个nothing值?您应该在VB.NET代码中使用一个可为null的类型作为参数类型,在这种情况下,
可为null(十进制)
,以匹配您的SQL数据类型。您应该在VB.NET代码中使用一个可为null的类型作为参数类型,在这种情况下,可为null(十进制)
,要匹配您的SQL数据类型。请使用可为空(十进制)
,如下所示:
Function GetCostAsString(val As Nullable(Of Decimal)) As String
' return empty string if val is Nothing, else do other stuff with decimal value.
If val.HasValue Then
' Do stuff here and return decimal value
Else
Return String.Empty
End If
End Function
注意:HasValue
属性等同于检查Decimal
是否为Nothing
False
表示它是无
,因此Else
返回一个空字符串。使用可为空(十进制)
,如下所示:
Function GetCostAsString(val As Nullable(Of Decimal)) As String
' return empty string if val is Nothing, else do other stuff with decimal value.
If val.HasValue Then
' Do stuff here and return decimal value
Else
Return String.Empty
End If
End Function
注意:
HasValue
属性等同于检查Decimal
是否为Nothing
False
表示它是Nothing
,因此Else
返回一个空字符串。您是否尝试将函数中的参数定义为可空(十进制)
?谢谢,效果很好。很好,我将其作为答案发布给其他人使用。您是否尝试将函数中的参数定义为可空(十进制)
?谢谢,效果很好。很好,我把它作为答案发布给其他人使用。