Solved

Saving to an ansi encoded text/csv file

Posted on 2014-09-10
21
259 Views
Last Modified: 2014-09-17
Have a web form and I'm populating an external file with the data.
When the submitted data contains a right-apostrophe, it lays down a ’ in it's place.
How can I (or can I) convert that so that it's the same character in the text file, or does it not support that character in ANSI encoding?
0
Comment
Question by:sirbounty
  • 11
  • 10
21 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40323411
"I'm populating an external file with the data."
How are you creating this file?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40325344
Streamwriter...

If File.Exists(filePath) Then
    Try
        Dim sw As StreamWriter = File.AppendText(filePath)
        sw.WriteLine(DataEntry.ToString)
        sw.Flush()
        sw.Close()
    Catch ex As Exception
    End Try
End If

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40325790
Try using encoding:

Using writer As New StreamWriter(fileName, append, New UTF8Encoding(False))
    writer.Write(saveString)
    writer.Close()
End Using

Open in new window

0
Technology Partners: 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!

 
LVL 67

Author Comment

by:sirbounty
ID: 40325915
Still adding the 3 character adjustment. :\
Though I did make some changes to the web.config last week... I"ll remove that and retry.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40325953
Unfortunately, it seems to end with the same results no matter what I try.
I'm not sure if I can change the encoding of the text file to something else.
This is still part of that other project that you've been helping me with.  This is the csv file that gets uploaded.
In order to have a match of the email subject, I need this to be what is submitted.  If it differs, it won't find the item.  My other thought was to truncate it at the invalid character - at least that gets me partial subject data...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326018
There are other encodings that you should try:

Default = ANSI
Unicode
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326050
Yeah, I had already given that a try.  One inserted spaces on either side of every character, and used a single funky character in place of the apostrophe.  I think ANSI replaced it with a ? character...

It's weird tho - if I open the file in notepad and choose file/save as - it shows ANSI.
I can copy/paste the data there with no problems.  Not understanding why I can't do that through code.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326063
" I think ANSI replaced it with a ? character"
That sounds more like ASCII, than ANSI.

What is the code for the apostrophe character?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326078
You mean - chr(146)?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326079
I didn't see an ansi encoding
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326085
That would be the extended ASCII character code, which is Latin-1 (ISO-8859-1).

Encoding.GetEncoding("ISO-8859-1")
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326086
Encoding.Default = ANSI code page
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326106
Are you saying I should use Encoding.GetEncoding("ISO-8859-1") in my streamwriter?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326111
Yes, I was trying to say, "Give ISO-8859-1 encoding a try with the StreamWriter".
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326131
Now that 'looks' better...
Original: don’t
Final: don't

But the first apostrophe is chr(146), the second is chr(39).
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326149
That is confusing.  I am not sure how to deal with that statement.  Is the character getting converted?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326186
"Don't" is the word that's tripping me up.   The apostrophe is a right-apostrophe (chr(146).
If I copy it from the email request and paste it into notepad/the csv it display correctly.

If I add it via my web form using a streamwriter, it converts it to a single apostrophe, chr(39).
I don't know that I want it converted - I'd rather it be the same exact character so that when the email is searched for in the database, it finds it by the exact same subject.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 40326205
One last experiment would be to try Encoding.Default instead of Latin-1 encoding with the StreamWriter.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40326241
That's not what is used when you don't specify encoding?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40326385
The default, without encoding is actually UTF-8 without byte-order marker (BOM).

StreamWriter constructor (stream):
   
  Public Sub New(ByVal stream As System.IO.Stream)
            MyClass.New(stream, StreamWriter.UTF8NoBOM, 1024, False)
        End Sub

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 40327673
Encoding.Default seemed to do the trick.  Thanks much! :^)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…

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