Solved

Saving to an ansi encoded text/csv file

Posted on 2014-09-10
21
232 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

20 Experts available now in Live!

Get 1:1 Help Now