how can I limit the # of characters to display for html.encode?
<%= Html.Encode(item.LastName.Substring(1,30))%>
error: Index and length must refer to a location within the string.
how can I limit the # of characters to display for html.encode?
<%= Html.Encode(item.LastName.Substring(1,30))%>
error: Index and length must refer to a location within the string.
You need to check the strings length is greater than 30, otherwise you are specifying a length which will fall off the end of the string...(I've also changed your start index to 0 assuming you didn't mean to leave out the first character)
<%= Html.Encode(item.LastName.Substring(0,
item.LastName.Length > 30 ? 30 : item.LastName.Length))%>
<%= Html.Encode(item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>
If you want to check for null, do this instead:
<%= Html.Encode(
item.LastName == null ? string.Empty :
item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>
You could also do something like
<%= Html.Encode(item.LastName.Substring(0, Math.Min(item.LastName.Length, 30)) %>
to save some bytes