Solved

Show only first 50 characters (Truncate string)

Posted on 2006-11-29
13
637 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 50
Footer for each row on Gridview 2 39
C# MVC Insert Multiple Row into DB 2 61
why JSON Array data not binding on Model ASP.NET ? 1 28
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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