Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-06
4
Medium Priority
?
168 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:
Meir Rivkin earned 800 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:Meir Rivkin
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 200 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

971 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