Solved

streamwriter problems

Posted on 2008-06-12
5
1,028 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No prob.  Glad to hear you have it fixed.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now