Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Show only first 50 characters (Truncate string)

Posted on 2006-11-29
13
Medium Priority
?
642 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
Technology Partners: 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 180 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 320 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

772 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