?
Solved

streamwriter problems

Posted on 2008-06-12
5
Medium Priority
?
1,071 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 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