Solved

Show only first 50 characters (Truncate string)

Posted on 2006-11-29
13
636 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
ID: 18036910
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
ID: 18036937
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
ID: 18036943
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mrong
ID: 18037455
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
ID: 18037509
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
ID: 18037603
Yes, I did that for sure.
0
 

Author Comment

by:mrong
ID: 18037609
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
ID: 18037682
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
ID: 18038119
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
ID: 18038158
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
ID: 18038199
It uses 'lpad' instead of 'left' in Oracle.
0
 

Author Comment

by:mrong
ID: 18038436
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
ID: 18041300
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

831 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