IIF Expression in RDLC report of ASP.NET project acting weird

75 Views Asked by At

I'm working on a ASP.NET project where I'm using RDLC report to generate reports. In a report in some field I'm using an expression like below:

=IIf(Fields!Day.Value >= 1 And Fields!Day.Value <= 7, WeekdayName(Fields!Day.Value), "invalid")

The problem is, If the value is between 1 to 7, it's working fine, but if the value is 0 it's printing #Error, which is unexpected. But If my expression is like below:

=IIf(Fields!Day.Value >= 1 And Fields!Day.Value <= 7, "True", "invalid")

Then, it works as expected, it printing invalid for value=0. Can someome point me out the issue?

1

There are 1 best solutions below

2
Alan Schofield On BEST ANSWER

The problem is the WEEKDAYNAME() function is being evaluated even if the weekday is out of range, hence the error.

You can fix it by passing a valid value to 'WEEKDAYNAME()' when it's an invalid value, this will never be returned but satisfies the function.

Change the exression to this and it will work

=IIF(
    Fields!Day.Value >= 1 And Fields!Day.Value <= 7, 
    WEEKDAYNAME(
        IIF(Fields!Day.Value >=1 AND Fields!Day.Value <=7, Fields!Day.Value, 1)
        )
    , "invalid"
    )