Reputation: 160
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!!!
Upvotes: 1
Views: 1020
Reputation: 44941
The problem that you are having with iif
is that the entire statement is evaluated every time, not just the true or false part. This means that the Remove(17)
is being fired every time, regardless of what the condition evaluates to.
You can prove this with the following code:
Dim sValue = IIf(Nothing, DoEval("true"), DoEval("false"))
Console.WriteLine("Final " & sValue)
Private Function DoEval(value As String) As String
Console.WriteLine(value)
Return value
End Function
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 iif
that we have banned its use in our applications.
You would be much better off switching to a traditional if then else
clause.
Upvotes: 1