Solved

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

Posted on 2013-02-06
4
162 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
ID: 38859034
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
ID: 38859083
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
ID: 38863117
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
ID: 38867256
Thanks - I didn't use the T-SQL solution but didn't know about SUBSTRING so thank you Akila :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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