Trouble with a conditional expression in Report Designer
Hello!
I'm attempting to create a simple report based on a layer in Geocortex. The issue I am encountering is in regards to a conditional statement that I've tried to construct to format a string field (so that it displays in a more user-friendly way in the report). The values of this field will always have strings of lengths 10, 11, 12 (or are Null) and are structured in a regular way. What I wanted to do was to test for the string length (and Null) and depending on the value, format it in a particular way.
So, for example, below is how the string looks in the field and how it should look on the report:
0284700289 ----> 2847/289
0284700289A ----> 2847/289A
AB0284700289 ----> 2847/289 (and another textbox to hold the 'AB' characters).
I created three textboxes (for now, just to test each condition statement separately) and put the following statements in the DataField property, based on the examples I found in the ActiveReports documentation:
=(FIELD.Length == 10)?(FIELD.Substring(2,4) + "/" + FIELD.Substring(7,3)):"N/A")
=(FIELD.Length == 11)?(FIELD.Substring(2,4) + "/" + FIELD.Substring(7,4)):"N/A")
=(FIELD.Length == 12)?(FIELD.Substring(3,4) + "/" + FIELD.Substring(9,3)):"N/A")
Unfortunately, in the report, if the field value is of 10 characters the last two statements return a "Control Script Failed" message, if it is 11 characters the last statement will return the same "Control Script Failed" message, if it is 12 characters all the statements work fine and put the expected value (of "N/A" or the formatted string).
I'm only new to C# and using the Report Designer, but it looks as though both the true-part and the false-part statements after the '?' are being evaluated, regardless of the what the equality test outcome is. From a bit of quick reading online, it seems that this is not supposed to happen in C# for the ?: expression, but it does happen for VB.NET IIF().
Are there any alternatives to the above so I can avoid this problem? Maybe using Trim/TrimStart type of methods instead? Or else, can one put 'If-Else' type statements in the DataField property of a textbox or does it have to be the above syntax? Or do I need to add some code to an event in the Script part of the Report Designer?
Any help appreciated :)
Cheers,
Maya.
(ps I was going to get around to including a test for Null, but wanted to get the above working first!)
-
Hi Maya,
If there's a chance that your data could be null, it's best to use the Script tab and deal with it there. Otherwise, you end up with very complex calculated fields.
I would use a case/switch statement to follow your logic.
in Detail1_Format:
TextBox textbox1 = (TextBox)rpt.Sections["Detail1"].Controls["TextBox1"]; string text = textbox1.Text; string firstHalf = string.Empty; string secondHalf = string.Empty; string separator = "/"; if (!string.IsNullOrEmpty(text)) { switch(text.Length) { case 10: // 0284700289 firstHalf = text.Substring(2,4); // 2847 secondHalf = text.Substring(7,3); // 289 break; case 11: // 0284700289A firstHalf = text.Substring(2,4); // 2847 secondHalf = text.Substring(7,4); // 289A break; case 12: // AB0284700289 firstHalf = text.Substring(3,4); // 2847 secondHalf = text.Substring(9,3); // 289 break; default: separator = ""; secondHalf = text; break; } textbox1.Text = string.Format("{0}{1}{2}", firstHalf, separator, secondHalf); }That's pretty verbose code, but it should be fairly easy to follow.
0 -
Hi Malcom,
Thanks so much for the response; what you suggested worked great. It was straightforward to read/understand, and helped to demystify the use of the Script tab a bit for me, which I was finding a little daunting to use at first :)
The only change I made was to the starting number in the Substring method for the "firstHalf" variable for case 10 and 11 (from 2 to 1) as from what I understand the count starts at 0, rather than 1 (I think this may have been a carry over from my original post, which I realised had the value of 2 instead of 1 as well :-/).
case 10: // 0284700289
firstHalf = text.Substring(1,4); // 2847case 11: // 0284700289A
firstHalf = text.Substring(1,4); // 2847Thanks again! :)
0
Please sign in to leave a comment.
Comments
2 comments