ASP(VB).NET character output
Posted: Fri May 23, 2008 10:06 pm
What I need to do is send data out through the browser in a downloadable text file. The client sends this file to a corporate printing company that parses the file out onto their premade forms. The file is tab delimited and the content contains newline characters. To preserve the newlines so that the line only breaks at the end and not in the middle of content, I need to replace them with the hex character "ff" which is supposed to be Chr(255) in VB. However, no matter what I try the end result always gives me "c3 bf".
Here is the relevant code.
Here is the relevant code.
Code: Select all
Public Sub Format_Export_Field(ByVal pval As String, ByRef pstr As System.Text.StringBuilder)
pval = Replace(pval, ControlChars.Back, String.Empty)
pval = Replace(pval, ControlChars.Cr, String.Empty)
pval = Replace(pval, ControlChars.Lf, String.Empty)
pval = Replace(pval, ControlChars.FormFeed, String.Empty)
pval = Replace(pval, ControlChars.Tab, String.Empty)
pval = Trim(pval)
pstr.Append(pval & ControlChars.Tab)
End Sub
Public Sub Generate_TXT_Renewal_Notice(ByVal batchID As String, ByVal renewalID As Integer, ByVal psumall As Integer)
Dim ds As System.Data.DataSet
Dim dv As System.Data.DataView
Dim output As StringBuilder
Dim clsRenewals As New TspEstimate.Renewal(m_user)
ds = clsRenewals.Get_Renewals_By_Batch(batchID, renewalID, psumall)
If ds.Tables.Count = 0 Then
Exit Sub
End If
Response.ClearContent()
Response.ContentType = "text/plain"
Response.Charset = "UTF-8"
Response.AppendHeader("content-disposition", "attachment; filename=" & _
"RenewalExport_" & Now.Year & Now.Month & Now.Day & ".txt")
dv = New System.Data.DataView(ds.Tables(0))
For Each dvr As Data.DataRowView In dv
output = New StringBuilder
'1
Format_Export_Field(dvr("branchname"), output)
'2
Format_Export_Field(LTrim(dvr("sourcestreet") & " ") & dvr("sourcestreet2"), output)
'3
Format_Export_Field(dvr("sourcecity"), output)
'4
Format_Export_Field(dvr("sourcephone"), output)
'5
Format_Export_Field(dvr("targetbusinessname"), output)
'6
Format_Export_Field(dvr("targetname"), output)
'7
Format_Export_Field(LTrim(dvr("targetstreet") & " ") & dvr("targetstreet2"), output)
'8
Format_Export_Field(dvr("target_city"), output)
'9
Format_Export_Field(dvr("targetstate"), output)
'10
Format_Export_Field(dvr("targetpostal"), output)
'11
Format_Export_Field(dvr("targetpostalex"), output)
'12
Format_Export_Field(dvr("servicebusinessname"), output)
'13
Format_Export_Field(dvr("servicename"), output)
'14
Format_Export_Field(LTrim(dvr("servicesitestreet") & " ") & dvr("servicesitestreet2"), output)
'15
Format_Export_Field(dvr("servicesitecity"), output)
'16
If dvr("renewaltext") Is System.DBNull.Value Then
output.Append(Chr(255), 4)
output.Append(Replace(dvr("renewaltext2"), ControlChars.NewLine, Chr(255)) & ControlChars.Tab)
Else
output.Append(Chr(255), 4)
output.Append(Replace(dvr("renewaltext"), ControlChars.NewLine, Chr(255)) & ControlChars.Tab)
End If
'17
Format_Export_Field(dvr("renewalamount"), output)
'18
Format_Export_Field(dvr("renewaltax"), output)
'19
Format_Export_Field(dvr("renewalamount") + dvr("renewaltax"), output)
'20
Format_Export_Field(dvr("programname"), output)
'21
Format_Export_Field(Month(dvr("renewaldate")) & "/" & Day(dvr("warrantydate")) & "/" & Year(dvr("renewaldate")), output)
'22
Format_Export_Field(dvr("accountnum"), output)
'23
Format_Export_Field(dvr("programid"), output)
output.Append(ControlChars.NewLine)
Response.Write(output.ToString)
Next
Response.Flush()
Response.Close()
End Sub