Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here is my sql command
select top 10 * from comment inner join list on list.id=comment.id where status=1 order by comment_time desc

I want to create like this
string str=comments.aspx?id="+dr["id"]+">"+dr["comment_body"]

In the comment body i need only 20 characters.

Please any one can help me.
Thanks in advance

i used the above solution but it shown error like
MSIL
Index and length must refer to a location within the string.
Parameter name: length



i am using data type as ntext
Posted
Updated 30-Jun-11 22:02pm
v5

This can be done in 2 places

1. in the query - better place if you have no other use of the full comment

using substring function, refer to
SUBSTRING (Transact-SQL)[^]

2. in c#

using String.Substring method, refer to
String.Substring Method (Int32, Int32)[^]
 
Share this answer
 
Comments
Pete O'Hanlon 1-Jul-11 3:57am    
And this, is the correct advice. Good job.
Christian Graus 1-Jul-11 4:24am    
Note that good advice is never accepted. Code that can be copied blindly by idiots, who have no idea why it works, or why it sometimes doesn't, is the order of the day.
Try this.
string str=comments.aspx?id="+dr["id"]+">"+dr["comment_body"].Substring(0, 20);
 
Share this answer
 
Comments
Abhinav S 1-Jul-11 3:29am    
We both posted at the same time. Even the seconds were same. :)
My 5.
Toniyo Jackson 1-Jul-11 3:33am    
:). It should be 20. 20 is the length
Pete O'Hanlon 1-Jul-11 3:56am    
He's taken 20 characters. The second parameter is the length, not the index to end at.
string str=comments.aspx?id="+dr["id"]+">"+Convert.ToString(dr["comment_body"]).SubString(0,20);
 
Share this answer
 
v2
Comments
Toniyo Jackson 1-Jul-11 3:34am    
My 5 too Abhinav. :)
vivekse 1-Jul-11 3:38am    
What happen if comment has less than 20 character? length must be checked before doing SubString.
Toniyo Jackson 1-Jul-11 3:46am    
OP has to work on that.
Toniyo Jackson 1-Jul-11 3:39am    
It should be 20. 20 is the length
Abhinav S 1-Jul-11 6:11am    
Yup my bad. Thanks for pointung that out.
string commentBody = (string)dr["comment_body"];
if(commentBody.Length > 20) commentBody = commentBody.Substring(0, 20);
string str = "comments.aspx?id="+dr["id"]+">" + commentBody;
 
Share this answer
 
try that way. Checking the length also if the length is greater than 20.

string str = "comments.aspx?id=" + dr["id"] + ">" + (Convert.IsDBNull(dr["comment_body"]) ? "" : dr["comment_body"].ToString().Substring(0, dr["comment_body"].ToString().Length > 20 ? 20 : dr["comment_body"].ToString().Length -1));
 
Share this answer
 
try this
VB
Dim st As String = "returing 20 characters from left side to the string."
        Dim chSt() As Char = st.ToCharArray()
        Array.Reverse(chSt)
        st = New String(chSt).Substring(0, 19)
        Label1.Text = st
 
Share this answer
 
Comments
BobJanova 1-Jul-11 5:46am    
In English locales, 'from the left' means from the beginning.
Henry Minute 1-Jul-11 7:38am    
This does not answer the OPs question.
Why are you reversing the string? Totally unnecessary.
Why Substring(0, 19)? the OP wanted 20 characters, not 19.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900