[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

streamwriter problems

Posted on 2008-06-12
5
Medium Priority
?
1,083 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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

834 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