Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index and length must refer to a location within the string?

I am trying to get the first 50 letters, so I used the subString function to get it.

As you can see, I used this code to get it:

<%# Eval("BannerDescription").ToString.Substring(1, 50)%>

But unfortunately it's not working and an error message is coming up:

Index and length must refer to a location within the string.

So is there any other way to fix it?

because the user is the one who is controlling the data entry! some times he gonna enter 10 letters other times maybe 1000 letter so how can i solve this ?

I tried them all but can we use it this way :

<%# IIf(Eval("BannerDescription").ToString().Length > 49, Eval("BannerDescription").ToString().Substring(0, 49), Eval("BannerDescription"))%>

Thanks.

like image 731
HAJJAJ Avatar asked Sep 13 '11 07:09

HAJJAJ


2 Answers

Maybe something like this -

<%# Eval("BannerDescription").ToString().Substring(0, Math.Min(Eval("BannerDescription").ToString().Length, 50)) %>
like image 174
Svetlin Panayotov Avatar answered Oct 05 '22 16:10

Svetlin Panayotov


<%# new string(Eval("BannerDescription").ToString().Take(50).ToArray()) %>
like image 27
Yuriy Rozhovetskiy Avatar answered Oct 05 '22 15:10

Yuriy Rozhovetskiy