Click to See Complete Forum and Search --> : Trim a datareader?


rpcarnell
10-02-2007, 06:15 PM
I have a script that creates a table using values it gets from a database.

The problem is that in areas like this one:

sb.Append(" alt= '" + dr["filename"] + "'\n");

the output is something like this alt='filename '

Notice the separation between filename and the last '.

I tried using Trim(), but the server tells me the object doesn't have a Trim method in it. Keep in mind dr is coming from this:

SqlDataReader dr = cmd.ExecuteReader();

which lacks a Trim() method.

What can I do? Turn everything into strings, trim it, and then put it together? If you look at the code below,you will notice that doing that would be throwing in a lot of code.



<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

public void Page_Load(Object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
OnLoadData(sender, e);
}


}

public void OnLoadData(Object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("DATABASE=landscapes;SERVER=MATRIX-YGC8NW1R;UID=Roddy;PWD=roddy");
String strCmd = "SELECT * FROM pictures";
SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{


MoreInfo.Text += BuildMoreInfoText(dr);

}

dr.Close();
conn.Close();
}



private String BuildMoreInfoText(SqlDataReader dr)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table>\n<tr>\n");
sb.Append("<td>\n<img src='images/"+dr["filename"]+"'\n");
sb.Append(" alt= '" + dr["filename"] + "'\n");
if (!(dr.IsDBNull(3)))
{
sb.Append("\n width='" + dr["width"] + "'");
}
sb.Append (" />");
sb.Append("\n</td>\n");
sb.Append("</tr>\n<tr>\n<td><b>");
sb.Append(dr["name"] + "</b>\n</td>\n</tr>");
sb.Append("\n<tr>\n<td>\n");
sb.Append(dr["description"]);
sb.Append("\n</td>\n</tr>\n</table>\n<br />\n");
return sb.ToString();
}

</script>
<html>
<head>
<title>Command Buttons</title> <style>HR {
COLOR: black; HEIGHT: 2px
}
.StdText {
FONT-WEIGHT: bold; FONT-SIZE: 9pt; FONT-FAMILY: verdana
}
.StdTextBox {
BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-SIZE: 9pt; FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true'); BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: verdana
}
.Shadow {
FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')
}
</style>
</head>
<body style="FONT-SIZE: 9pt; FONT-FAMILY: arial" bgcolor="ivory">
<!-- ASP.NET topbar -->
<h2>Command Buttons
</h2>
<asp:Label id="MoreInfo" runat="server"></asp:Label>
</body>
</html>

Cstick
10-02-2007, 09:24 PM
The indexor for a datareader returns the type object, which doesn't have a trim method of course. You can use the methods of the datareader like, dr.GetString("ColumnName").Trim(). Or you can use the ToString method of the object type and then trim, dr["ColumnName"].ToString.Trim().