Solved

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

Posted on 2013-02-06
4
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

631 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