Data Reader - Only want to display first 20 characters

Newbie81

Member
Joined
Nov 7, 2006
Messages
12
Programming Experience
Beginner
Hello All,

I have created a table in sql, and want to display a particular row, but only want to display the first 20 characters of it not the full message. How would u do this by using th data reader?

This is what i have done:


Dim cmd As SqlCommand = SqlConnection1.CreateCommand
cmd.Connection = SqlConnection1
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT * FROM Forum "
cmd.CommandText &= "WHERE receiver ='" & Session("Email") & "'"


Dim rowNumber As Int16
Response.Write("<BR>")
Response.Write("<b>Your Messages:</b>")
Response.Write("<Table border=1><tr><td>From</td><td>Message</td></tr>")
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.Default)

Do While dr.Read = True

Response.Write("<tr>")

Response.Write("<td>" & dr.Item("Sender") & "</td>")
Response.Write("<td>" & dr.Item("Message") & "</td>")
Response.Write("<td><a href='MessageReceive.aspx?read=" & dr.Item("Message") + "&sender=" & dr.Item("Sender") & "'>Read</a></td></tr>")
'

Loop

I thank you all you experts in advance.
 
Thank you for the reply. In this case, how would i use the ToSubstring function? Sorry I am a complete beginer.

Thank you very much
 
Hmm.. rather than transferring a megabyte of text from the db and having the client truncate it to 20 characters, i would get the db to do it. In oracle the sql would be like:

SELECT SUBSTR(message, 1, 20) as intro_msg FROM forum


Find out what the syntax is for your database and give it a go :)
 
Back
Top