Link to home
Start Free TrialLog in
Avatar of Lorna70
Lorna70

asked on

How can I display a summary of info from a Text field in DB?

Hi I want to display only the first 200 chars of text stored in the DB as a text field.  How do I do this so that it is cut off after a word and not in the middle of a word??  Also, I am displaying the text in a GridView.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you want that the total of characters after the substring will be always less or equal to 200 then you need something a bit different:

string longTxtFromDB = "extremely long text more than 200 characters ... .. . . ..  ..";
var st = new string(longTxtFromDB.Reverse().ToArray());
                int i = st.IndexOf(' ', longTxtFromDB.Length - 20);
               string cutOffString = longTxtFromDB.Substring(0, longTxtFromDB.Length-i);

Open in new window


btw, i used 20 just for testing, you should first check if the string length is more than 200:

string longTxtFromDB = "extremely long text more than 200 characters ... .. . . ..  ..";
if(longTxtFromDB.Length> 20){
var st = new string(longTxtFromDB.Reverse().ToArray());
                int i = st.IndexOf(' ', longTxtFromDB.Length - 20);
               string cutOffString = longTxtFromDB.Substring(0, longTxtFromDB.Length-i);
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lorna70
Lorna70

ASKER

Thanks - I didn't use the T-SQL solution but didn't know about SUBSTRING so thank you Akila :-)