Solved

streamwriter problems

Posted on 2008-06-12
5
1,045 Views
Last Modified: 2008-06-20
I am using io.streamwriter.write to output database fields to a text file. I am getting some strange characters appearing in the text file, for example where there is an apostrophe in a line of text the output file shows a series of garbage characters (see below)

Original text:
organisation's

text from output file:
organisationââ¬â¢s

it looks like it's converting the apostrophe to ââ¬â¢

This is also happening with other special characters.

here is my code:

 'itterate through tbl_upload and write out csv test file
        Dim file As String = "c:\local_audits\out.txt"
        Dim sw As New IO.StreamWriter(file)

        'Loop through each row/field and output it to a text file
        '    separating each field by $ and delimiting rows with ^

        Form4.Label2.Text = "Creating Temporary File"
        Form4.Refresh()
        Dim uploadrows As Integer
        uploadrows = tbl_upload.Rows.Count - 1
        Dim aa As Integer

        For aa = 0 To uploadrows
            For a = 0 To 116
                If a < 116 Then
                    If IsDBNull(tbl_upload.Rows(aa).Item(a)) Then
                        sw.Write("\N$")
                    Else
                        sw.Write(CStr(tbl_upload.Rows(aa).Item(a)) + "$")
                    End If
                Else
                    If IsDBNull(tbl_upload.Rows(aa).Item(a)) Then
                        sw.Write("\N")
                    Else
                        sw.Write(CStr(tbl_upload.Rows(aa).Item(a)))

                    End If

                End If
            Next
            sw.Write("^")
        Next
        'cleanup
        sw.Close()
        sw.Dispose()

does anyone have any ideas as to why? and how it can be cured.
0
Comment
Question by:Advectus
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 21768868
Can you print the value to the immediate window (Debug.Print or Debug.WriteLn, depending on whether your using VB6 or VB.Net) to see if it's something that's happening in the stream or something that's coming out of the database?
0
 
LVL 1

Author Comment

by:Advectus
ID: 21768931
It's definately from the stream. I have checked the contents of the database and the content of the datatable before writing out to the text file. I think it may be to do with character set encoding ???
0
 
LVL 10

Expert Comment

by:Clif
ID: 21769036
It's possible.  But the data is handed off to several internal things between the time it's taken from the database and the time it's written to the stream.  Checking the contents of the database and it's datatable tells you nothing except that the data was valid *before* it entered VB.

Have you tried creating a constant that contains an apostrophe and writing it to the text file to see if it writes garbage as well?
0
 
LVL 1

Accepted Solution

by:
Advectus earned 0 total points
ID: 21769248
I have fixed it, had to change encoding as follows:

Dim sw As New IO.StreamWriter(file)
 to
Dim sw As New IO.StreamWriter(file, False, System.Text.Encoding.UTF8)

I also had to specify the character set in my sql statement (LOAD DATA LOCAL INFILE.........CHARACTER SET utf8.....)

but thanks anyway.
0
 
LVL 10

Expert Comment

by:Clif
ID: 21769268
No prob.  Glad to hear you have it fixed.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question