C# RDLC 报告未正确检测 NULL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14276648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:17:00  来源:igfitidea点击:

RDLC report doesn't detect NULL values correctly

c#reportingrdlc

提问by NDraskovic

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

我在生成 .rdlc 报告时遇到问题。其中一列具有以下表达式:

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?

当 MyColumn 的值不是 时NULL,报表正确显示该值,但当它是NULL(或转换为 int 类型时为0)时,报表返回#Error。奇怪的是,当我删除 if 函数并仅显示该字段的值时,报告显示 0 或空白(它不返回错误)。我怎样才能解决这个问题?

采纳答案by Deruijter

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.

如果没有任何效果,您还可以检查代码中的 NULL 值,然后将该值设置为 0 或 -1(左右)。然后在您的 RDLC 报告中,您可以检查它。

回答by Gianni B.

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

我认为您必须在 BEFORE 之前检查您的字段是否为空,否则您将在比较中得到错误(NULL大于 0?-> 错误!)

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:

此外,如果您的字段已经是数字,则不必使用 CInt(""),否则公式将为:

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

回答by Hossein

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 Leninstead of IsNothing, Is Nothing, or the CInttrick.

根据@Deruijter 的回答下的评论,如果该表达式if (dr.ItemArray.GetValue(15).ToString() == "")对您有效且没有错误,那么您的字符串不是NULL; 您的数据集中只有空(即零长度)字符串。在这种情况下,在RDLC表达你会使用Len,而不是IsNothingIs NothingCInt伎俩。

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

如果这些值是 real NULL,则一次IsNothing检查就足够了。

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

还请记住,Or在使用 VB .NET 的 RDLC 表达式中,不会短路orVB .NET 中与 C# 或 C 类似的等效短路运算符是OrElse运算符。(不过,这似乎不会影响您在这里)。