Solved

Saving to an ansi encoded text/csv file

Posted on 2014-09-10
21
225 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
varchar (max) 5 28
Whats wrong with this query 21 39
How do see the validation errors on ASP.NET MVC 4 26
Adjust the position 3 0
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

17 Experts available now in Live!

Get 1:1 Help Now