RDLC report doesn't detect NULL values correctly

自闭症网瘾萝莉.ら 提交于 2019-12-23 12:06:39

问题


I have a problem with generating .rdlc report. One of the columns has this expression:

IIF(CInt(Fields!MyColumn.Value) = 0 or Fields!MyColumn.Value is nothing,"Unknown",Fields!MyColumn.Value)

I've also tried to use that field as a string:

=IIF(IsNothing(Fields!MyColumn.Value) or Fields!MyColumn.Value is nothing,"Unknown",Fields!MyColumn.Value.ToString())

When the value of MyColumn is not NULL, the report displays the value properly, but when it is NULL (or 0 when it's converted into int type) the report returns #Error. The weird thing is that when I remove the if function and display only the value of that field, the report displays 0 or blank (it doesn't return the error). How can I fix this?


回答1:


You could try validating without a comparison:

=IIf(Fields!YourColumn.Value
    , Fields!YourColumn.Value
    , "Unknown")

Or reversing your check (check if it exists, instead of checking if it doesn't exist):

=IIf(Fields!YourColumn.Value > 0
    , Fields!YourColumn.Value
    , "Unknown")

Also, I'm not sure, but it may have something to do with using different value types in the same column. Try using the same value type for an entire column. For example, only output strings, or only output ints.

If nothing works you can also check for NULL values in your code and then set the value to 0 or -1 (or so). Then in your RDLC report you can check on that.




回答2:


I think that you have to check BEFORE if your field is null or not, otherwise you will get the error in the comparison (NULL greater then 0? -> error!)

So your formula must be:

=IIF(IsNothing(Fields!MyColumn.Value) or CInt(Fields!MyColumn.Value) = 0,"Unknown",Fields!MyColumn.Value)

I would try also the Switch function:

=Switch(
    IsNothing(Fields!MyColumn.Value), "Unknown", 
    Fields!MyColumn.Value = 0, "Unknown", 
    Fields!MyColumn.Value > 0, Fields!MyColumn.Value, 
    )

Also, if your field is already a number you don't have to use CInt(""), otherwise the formula will be:

=Switch(
    IsNothing(Fields!MyColumn.Value), "Unknown", 
    CInt(Fields!MyColumn.Value) = 0, "Unknown", 
    CInt(Fields!MyColumn.Value) > 0, Fields!MyColumn.Value, 
    )



回答3:


According to comments under @Deruijter's answer, if the expression if (dr.ItemArray.GetValue(15).ToString() == "") works for you without errors, then your strings are NOT NULL; you just have empty (i.e zero-length) strings in your dataset. In that case, in RDLC expression you would use Len instead of IsNothing, Is Nothing, or the CInt trick.

If the values were real NULL, a single IsNothing check was sufficient.

Also remember, Or in RDLC expressions that use VB .NET does NOT short-circuit; the equivalent short-circuiting or operator in VB .NET that works like C# or C is the OrElse operator. (That doesn't seem to affect you here, though).



来源:https://stackoverflow.com/questions/14276648/rdlc-report-doesnt-detect-null-values-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!