Solved

VB.Net - Writing Strings to a comma delimited file

Posted on 2014-11-20
6
216 Views
Last Modified: 2014-11-20
I am attempting to write data out to a comma delimited file using a For - Next loop.
Some of the strings in the array use a double quote ( " ) as you would use to indicate Inches. The first few elements of the array do not have the double quote and they work fine, however, once I reach an element that contains the double quote the code treats it differently and I end up with a long string that does not handle the comma as a delimiter.

Here is the code:
Dim objWriter As New System.IO.StreamWriter(MeasDataFileName, True)

For j = 0 To maxAinIx - 2
    objWriter.Write(AinUnit(j, UnitsEMU))
    objWriter.Write(",")
Next
objWriter.WriteLine(AinUnit(j, UnitsEMU))

objWriter.Close()


I need help to ensure that the code will treat each element as a separate entry and no have the double quotes mess up the output.

Thanks,
Charlie
0
Comment
Question by:charlieb01
  • 2
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40454927
What does the code for your AinUnit look like.  This is most likely what you want to change so that it can properly handle double-quotes.

-saige-
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
ID: 40454935
Can you use the replace function and drop a chr(34) in there?

    objWriter.Write( Replace(AinUnit(j, UnitsEMU), chr(34), chr(34) & chr(34) & chr(34) )
0
 

Author Comment

by:charlieb01
ID: 40454960
The values in the AinUnit array come from a database. Some of them use the degrees symbol (for temperature), some use the double quotes (for inches - as in barometric pressure: "Hg) and some don't use any special symbol (such as the letter W  for watts). So I don't know how I can replace just the double quotes because it only affects some of the values.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40454982
So then the question is where do they get loaded into the array.  You want to modify setting the value in the array so that if a double quote is used you either:
1.  Replace it with an escape sequence (as shown by sirbounty).
2.  Encode it into another form (i.e. HTML Encoding)

The method used depends upon what is going to be receiving this file and how it processes it.  If you expect the double quotes to appear as double quotes in the file, then sirbounty has provided the way to do it, now we just need to know where to do it.

-saige-
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40455063
The replace function won't do anything if there are no quotes in the results...did you try the code?
0
 

Author Comment

by:charlieb01
ID: 40455668
The replace function did not work and I have run out of time to solve this problem. So, I am re-writing the database fields to use 'in' (for inches) to replace the double quotes.

Thank you for your help. Perhaps I will get a chance to revisit this issue again.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Write to file when application closes (by logoff) - VB.NET 1 37
Crystal reports vb.net 2 52
Convert datetime to time string 10 28
SSRS 2016 Rendering HTML tables 3 25
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

789 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