Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Saving to an ansi encoded text/csv file

Posted on 2014-09-10
21
Medium Priority
?
338 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

578 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