Solved

Show only first 50 characters (Truncate string)

Posted on 2006-11-29
13
634 Views
Last Modified: 2008-02-01
Greeting,

I have an asp.net web application which will display data on the web. I use repeaters to show the data. I want to limit the characters of one column(field) so it only display the first 50 characters and it will add "..." at the end.

Please provide sample code.

Thanks in advance.

-M
0
Comment
Question by:mrong
  • 6
  • 5
  • 2
13 Comments
 
LVL 5

Expert Comment

by:Collindsouza
Comment Utility
Hi  mrong,
The best Adviceis to retrive the column data from the database as a 50 Charecter string as..

SELECT LEFT(ColumnName, 50)  + '...' AS FirstFityLettersOfColumnLastName
FROM TABLENAME

If you need to show the full text as well then you can add that in the toolTip property of the repeater control.. for that you need to retrive it as follows..

SELECT LEFT(ColumnName, 50)  + '...' AS FirstFityLettersOfColumnLastName ,ColumnName
FROM TABLENAME

and in the repeater
<asp:HyperLink ID="HyperLink1" ToolTip='<%#DataBinder.Eval(Container.DataItem, "ColumnName")%>'
                                     Text='<%#DataBinder.Eval(Container.DataItem, "BlockName")%>' runat="server" />

0
 
LVL 5

Expert Comment

by:Collindsouza
Comment Utility
oops.. my bad.. Lots of typo error in my first post..

Please read this one... Sorry about that..

The best Advice is to retrive the column data from the database as a 50 Charecter string..

SELECT LEFT(ColumnName, 50)  + '...' AS FirstFiftyLettersOfColumnName
FROM TABLENAME

If you need to show the full text as well then you can add that in the toolTip property of the repeater control.. for that you need to retrive it as follows..

SELECT LEFT(ColumnName, 50)  + '...' AS FirstFityLettersOfColumnLastName ,ColumnName
FROM TABLENAME

and in the repeater
<asp:HyperLink ID="HyperLink1" ToolTip='<%#DataBinder.Eval(Container.DataItem, "ColumnName")%>'
                                     Text='<%#DataBinder.Eval(Container.DataItem, "FirstFiftyLettersOfColumnName")%>' runat="server" />


<asp:HyperLink ID="HyperLink1" ToolTip='<%#DataBinder.Eval(Container.DataItem, "ColumnName")%>'
                                     Text='<%#DataBinder.Eval(Container.DataItem, "FirstFityLettersOfColumnLastName ")%>' runat="server" />

0
 
LVL 7

Expert Comment

by:nepali
Comment Utility
I guess this will easy to do on the SQL end. The query that gets the dataset should have this logic. for instance in sql server can do the following to retrieve desc column from styles table.

select case
when len(desc)  > 30 then left(desc,30) + '...'
else
desc
end
from Styles

0
 

Author Comment

by:mrong
Comment Utility
Collindsouza,

I modified the sql statement in code-behind but got error "invalid column name"

Please see my sql below.
SELECT LEFT(ColumnName, 50)  + '...' AS MyColumn
FROM TABLENAME

I also modified the asp code as you suggested above.
0
 
LVL 5

Expert Comment

by:Collindsouza
Comment Utility
mrong

Please Replace ColumnName and TABLENAME with the Actual names of the column and the table you are refering to in your database..

For example if the column i was refering to was "Address" and the tableName was "Employees" then the SQL statement would change to

SELECT LEFT(Address, 50)  + '...' AS MyColumn
FROM Employees

Collin
0
 

Author Comment

by:mrong
Comment Utility
Yes, I did that for sure.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mrong
Comment Utility
My sql is similar to

SELECT LEFT(Address, 50)  + '...' AS MyColumn,column2,column3
FROM Employees
0
 
LVL 5

Assisted Solution

by:Collindsouza
Collindsouza earned 45 total points
Comment Utility
mrong

U executed the query at my end and it works fine... please compare every column names from your Query with those in the database to see if there is an exact match..
better still try executing only the following..

SELECT LEFT(Address, 50)  + '...' AS MyColumn
FROM Employees

and see if it returns any errors..

If it doesnt then the problem is with the other column names..

0
 

Author Comment

by:mrong
Comment Utility
I tried your code in sql-plus and it returns me the same error message. My database is Oracle8.

Thanks.
0
 
LVL 7

Accepted Solution

by:
nepali earned 80 total points
Comment Utility
Hi mrong... the Left Function is specific to Sql server.. And i doubt that oracle8 supports ansi sql. Hence Case statement wouldn't work. Try using decode function of oracle
0
 

Author Comment

by:mrong
Comment Utility
It uses 'lpad' instead of 'left' in Oracle.
0
 

Author Comment

by:mrong
Comment Utility
The solution below works.

In Sql:
SELECT lpad(Address, 50) AS MyColumn
FROM Employees

in asp.net
<td><div align=left><%#DataBinder.Eval(Container.DataItem, "MyColumn") + "..."%></td>

Thank you all:-)


0
 
LVL 5

Expert Comment

by:Collindsouza
Comment Utility
ahhhhh.... i did'nt know you were using Oracle8...

sorry about that...

Now that your solution works.. I'm happy for you..

You can now close this thread.. accept our solution and split points between me n Nepali.. ohh dont forget to award Grade A ;) it helps
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now