Skip to main content

Trouble with a conditional expression in Report Designer

Comments

2 comments

  • Permanently deleted user

    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
  • Permanently deleted user

    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);  // 2847

    case 11:  // 0284700289A

     

          firstHalf = text.Substring(1,4);  // 2847

    Thanks again! :)

    0

Please sign in to leave a comment.