Solved

streamwriter problems

Posted on 2008-06-12
5
1,033 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now