4

When we deploy a report SSRS generates the following error:

There is an error on line 58 of custom code: [BC30201] Expression expected

However, the report works fine on preview mode and displays the field correctly.

The custom code segment here is also written and tested in visual studio.

Here's the custom code:

If (evaluationDate.Day = 31) Then '* affected line 
    returnValue.Append(String.Format("{0}{1:dd.MM}{2}", _
        If(index = 2, " und ", String.Empty), _
        New DateTime(evaluationDate.Year, evaluationDate.Month, lastDayOfMonthDictionary(evaluationDate.Month)), _
        If(index = 2, ".", String.Empty)))

End If

As you can see, the problem is by an IF..THEN block. evaluationDate is of type DateTime and the value is either equal to the DateTime argument startdate or six months ahead of startDate - which is a datetime argument in the function's signature.

I cannot see what's wrong with this and I need to know what I can do to resolve this issue. Any ideas?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Eon
  • 3,833
  • 10
  • 46
  • 75
  • 6 months ahead is June (which has 30 days), so instinctively I'd fixate on that 31-day magic number there to start debugging with. Whether that's actually the root cause I'm not sure. – Nathan Tuggy Dec 11 '14 at 08:26
  • Remove the `()` around the statement? `If evaluationDate.Day = 31 Then` – Visual Vincent Dec 11 '14 at 08:27
  • @VisualVincent I tried that. The error turned into `invalid definition`. @NathanTuggy, I have written and tested this in Visual studio, when the date was 31 December on `startdate` and 6 months were added, the evaluation date was 30 June - that didn't cause issues. as I say, this error only happens during deployment. on Preview, it works correctly – Eon Dec 11 '14 at 08:31
  • Nevermind. @VisualVincent. I retried it, this time it gave me `expression expected`. This is starting to confuse me xD – Eon Dec 11 '14 at 08:33

1 Answers1

6

The conclusion - SSRS has an evil way to deal with ternaries, even though it fully expects VB code in the custom code segment.

This error I had received was misdirecting, and pointed to the wrong line even - it was the line right below this one I marked in the question.

Notice the Usage of ternaries : If(index = 2, " und ", String.Empty) - SSRS tries to run the If-ternary (as you would declare it in VB.NET) as a If..Then block - and because no Then is found, and there are multiple arguments comma-separated, this muddles SSRS and thus it prints Expression Expected

The way to fix this issue is the Traditional SSRS Expressional way

'Instead of this:
If(index = 2, " und ", String.Empty)

'Use This:
IIf(index = 2, " und ", String.Empty)

This should still allow you to preview your code (even the normal If allowed you to preview, but breaks during deploy)

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Eon
  • 3,833
  • 10
  • 46
  • 75
  • Upvoted. I'm annoyed because I just got bitten by this - I can't use the IIF operator because I need short-circuiting, but I don't have a choice because SSRS sucks. Sigh. Guess you can't win 'em all, huh? –  Apr 17 '15 at 18:50
  • 1
    @jedd.ahyoung The Custom Code of a report can handle plain old VB.NET, so why not write a helper method instead using old school if-statements (which supports short-circuiting) and reference those helpers in your expressions toolbox with `... & !Code.methodName(argument) & ...` – Eon Apr 20 '15 at 11:16
  • Thank you guys, this saved my time. – Saxophonist Jun 20 '16 at 11:26