Solved

Show only first 50 characters (Truncate string)

Posted on 2006-11-29
13
638 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VStudio 2015 WebAPi works fine from local debug not working from published IIS7 1 39
asp.net mvc 2 44
What are MicroServices? 4 69
Ajax calendar distorted 4 24
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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