Solved

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

Posted on 2013-02-06
4
160 Views
Last Modified: 2013-02-08
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.
0
Comment
Question by:Lorna70
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 200 total points
Comment Utility
assume you have the text insome variable after your query succeeds:
string longTxtFromDB = "extremely long text more than 200 characters ... .. . . ..  ..";
int i = longTxtFromDB.IndexOf(' ', 200); //find the index of first occurrence of space from the 200th characters
string cutOffString = longTxtFromDB.Substring(0,i);//substring the original txt

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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

0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 50 total points
Comment Utility
while fetching and binding itself u can get only specified length of values from the database


it takes 10 characters

SELECT  SUBSTRING(country,1,10) as country
FROM  countries 

Open in new window

0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now