Strange bug in StringBuilder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I am using StringBuilder to compose an email. The body of email consists of
a table for which data has to be taken fron SQL Server.

I use appen method to add text to the StringBuilder variable. When I am
finished I call ToString function to get the string.

The problem is that many times, it changes the text I appended to it. Like
when I append

<TH vAlign=top noWrap width=30>ABC<BR>10<BR>5<br>05</TH>

it some time changes that to
<TH vAlign=top noWrap width=30>Tue<BR>10<BR>5< br>05</TH>

some time it does this with other HTML attribute (mostly with < >). This
happens occasionaly. Sometime it works fine and sometime not.

I tried using the String instead of StringBuilder but the problem is still
there.

Please help me.
 
Once it changed:

ABC<BR>6<BR>5<BR>05</FONT></TH>

to

<TH vAlign=top noWrap width=30><FONT face=Arial size=2 r="#ffffff"
colo>ABC<BR>6<BR>5<BR>05</FONT></TH>

strange behaviour

Thanks in advance
 
1. What version of the Framework?
2. What is the specific code you are using?

The StringBuilder is a character array. I am not sure how it would change
ABC to Tue (change char values), but I could see a possibility of changing
the order of letters. If it is changing char values, I would look at encoding
first. The order of letters sounds like a genuine bug.

As you have quotes, make sure they are properly escaped.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Sorry for my one mistake. The pattern ABC was replaced by me. The project I
am working on is confidential, so I tried to change Tue to ABC, but forget in
other place.

But, StringBuilder did changed
<FONT face=Arial size=2 color="#ffffff">

to

<FONT face=Arial size=2 r="#ffffff" colo>

I am using VStudio 2003 Vb.net and building for .net 1.1
 
ramata said:
I am using StringBuilder to compose an email. The body of email consists of
a table for which data has to be taken fron SQL Server.

I use appen method to add text to the StringBuilder variable. When I am
finished I call ToString function to get the string.

The problem is that many times, it changes the text I appended to it.

I find that very hard to believe.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
The code is Vb.Net code. Basically two procedures. One composes email and
other sends it. This can be put in any Vb.net class and calling SendEmail()

Public Sub GetEmailBody(ByRef strHTMLMessage As StringBuilder)

Dim strQuery As String
Dim Adodc1 As ADODB.Recordset
strHTMLMessage.Remove(0, strHTMLMessage.Length)

' create HTML message
strHTMLMessage.Append("<html><head><title>My page</title> " _
& "<style type=""text/css"">th {color:
#ffffff;font: bold 12px arial} td.b {color: #ffffff;font: bold 12px arial}
</style></head><body>" _
& "<table align=center border=1 cellspacing=0
cellpadding=1 style='border-collapse: collapse' bordercolor=#777777>" _
& "<tr bgcolor=#ff0000>" _
& "<th nowrap valign=top align=left
width=200>col1</th>" _
& "<th valign=top>col2</th>" _
& "<th nowrap valign=top>col3<br>one<br>two</th>" _

Adodc1 = New ADODB.Recordset


Dim strQuery As String = "SELECT * FROM sample"

' conn is global Adodb.Connection
Adodc1.Open(strQuery, conn)

If Not Adodc1.EOF Then

Do Until Adodc1.EOF

strHTMLMessage.Append(vbCrLf & "<tr bgcolor=#ffcccc>")

strHTMLMessage.Append("<td nowrap align=left>" &
tmpPlay.StationName & "</td><td nowrap align=left>" &
tmpPlay.PresentationRegion & "</td>")

loop

strHTMLMessage.Append("</tr>")

Next

' end of table
strHTMLMessage.Append("</table>")
strHTMLMessage.Append("</body></html>")

End Sub

Public Sub SendEmail()

Dim strBody As New StringBuilder

Dim mailmsg As CDO.Message
Dim Conf As CDO.Configuration
mailmsg = New CDO.Message
mailmsg.Configuration.Load(CDO.CdoConfigSource.cdoIIS)

eMailFrom = "(e-mail address removed)"


GetEmailBody(strBody)
eMailSubject = "testing"

With mailmsg
.From = eMailFrom
.Subject = eMailSubject
.To = recp(i).email
.HTMLBody = strBody.ToString
.Send()
End With

End Sub
 
Back
Top