I’m trying to strip characters from the end of a string using .tostring.remove(n). with the code below the IIf statement confirms the string is ISNullOrEmpty and returns True or False. If true is returned the string of "" is placed in the aspx page (this works fine), the problem arises in the second part which is triggered when the IsNullOrEmpty returns False.
Upon a False value the strings length is checked, if the string is equal to or more than 17 characters long the a True value is returned and the string is truncated using the .ToString.Remove(n) if a False value is returned then the string is left untouched.
the problem is that if the string is 1 character or 20 characters long the following error keeps occurring:
startIndex must be less than length of string
here is the code block:
<%# IIf(String.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(Container.DataItem, "SponsorEmail"))),
"",
IIf(Convert.ToString(DataBinder.Eval(Container.DataItem, "SponsorEmail")).Length > 17,
Eval("SponsorEmail").ToString.Remove(17),
Eval("SponsorEmail")))%>
The database tables the databinder is drawing it’s data from does allow NULLS, and it only happens when a NULL or Empty string is present for any given record.
Help Please, it’s driving me nuts!!!
The problem that you are having with
iifis that the entire statement is evaluated every time, not just the true or false part. This means that theRemove(17)is being fired every time, regardless of what the condition evaluates to.You can prove this with the following code:
In this case, you will see both true and false written to the output window.
We had so many issues introduced through the use of
iifthat we have banned its use in our applications.You would be much better off switching to a traditional
if then elseclause.