Writing to a CSV file

A

Aaron

Hello,

I have a small application that I need to save data from 7 text boxes in to
a csv file. This will entail btnNext_Click function that will create a new
csv file and enter the 7 data fields in the csv file; as well, each click on
next will return carriage to a new line.

Client does not what his data in an Access database(he hates Access with a
passion)--he feels more comfortable with Excel. This data will be no greater
than 1000 lines per use.


TIA

Aaron
 
G

Guest

Hello!
you didn't ask any question, you just wrote what your application is doing.
if you want answer write your problem
guy
 
D

Dennis Myrén

protected void btnNext_Click ( object sender, EventArgs e )
{
StreamWriter sw = new StreamWriter(filename, true);
sw.WriteLine(string.Concat
( textBox1.Text
, textBox2.Text
, textBox3.Text
, textBox4.Text
, textBox5.Text
, textBox6.Text
, textBox7.Text ));
sw.Close();
}

But maybe use the StreamWriter in a module global scope,
if you want to avoid opening the file for each click on the Next button,but
rather initialize
it once and close it once.
 
A

Aaron

guy,

Sorry

I want to know how too do the following
1) create a .csv file--there will be a single text box on top of the form
that would have the name of the file.
2) format the line of data from the 7 text boxes then carriage return on
each time btnNext is clicked.

tia

Aaron
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

This is not a good solution, what if some of the values have commas ? they
will be interpreted as field separator.

Take a look at the opennetcf.org data provider, it does implement it and is
a proven solution.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
N

Nick Malik

Close.... oh so close...
two problems:
(1) the posted code fails to place commas between the values
(2) the posted code fails to quote the strings (since these are copied from
text boxes without type checking, the values may contain all kinds of
things).

here's a slightly better way. However, this will fail if the text boxes
contain a double-quote character (").

public void AppendLineToCSV()
{
using (StreamWriter sw = new StreamWriter(filename, true))
{

sw.WriteLine("/"{0}/",/"{1}/",/"{2}/",/"{3}/",/"{4}/",/"{5}/",/"{6}/"",
textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text,
textBox5.Text, textBox6.Text, textBox7.Text);
}
}

Note: your customer will probably also want the ability to read from the
list of items in the file and rewrite particular items, so this solution is
partial at best. You are going to want to manage a collection of "rows"
where each row contains your seven columns. Then, simply write out all of
the rows in the collection when it comes time to save the data to a file.

This isn't really Excel format. CSV is a comma-delimited format that Excel
is comfortable reading for data transfer, but it cannot handle formulas or
formatted text or other features of excel at all...

Note: if your user will be modifying the data using Excel, you will need to
validate it when you open a file, to make sure that he hasn't changed it in
some way that makes it "less useful" to your application (like changing a
data type, or adding a blank line, etc).

Good Luck, I hope this helps,
--- Nick
 
A

Aaron

Dennis,

Thanks.

Aaron


Dennis Myrén said:
protected void btnNext_Click ( object sender, EventArgs e )
{
StreamWriter sw = new StreamWriter(filename, true);
sw.WriteLine(string.Concat
( textBox1.Text
, textBox2.Text
, textBox3.Text
, textBox4.Text
, textBox5.Text
, textBox6.Text
, textBox7.Text ));
sw.Close();
}

But maybe use the StreamWriter in a module global scope,
if you want to avoid opening the file for each click on the Next button,but
rather initialize
it once and close it once.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
boxes
 
A

Aaron

Ignacio,

Thanks.

Aaron

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

This is not a good solution, what if some of the values have commas ? they
will be interpreted as field separator.

Take a look at the opennetcf.org data provider, it does implement it and is
a proven solution.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




create
 
A

Aaron

Nick,

Thanks for the information. For the commas and qoutes in the textboxes that
is not ever going to be the case.
This is a data entry form, there will not be any data modification or
validation at this point of the process(dont ask).

Managing the "rows"?, What would that entail, a datagrid?

TIA

Aaron
 
N

Nick Malik

Hi Aaron,

I'm not particularly fond of datagrids most of the time, although you could
make a case for it.

No. I'm thinking ahead, to what you didn't say:
If the user has the ability to add a row to a CSV file, and the user hates
Access, then the user may want to be able to:
1) view the contents of one or more existing rows
2) edit an existing row
3) delete an existing row

In this case, you don't have one row. You have a list or collection of
rows. each row has seven column values.

Given the small number of total rows, I'm assuming you could do this with a
simple arraylist collection where each element is a simple string array.
Not the most efficient structure, but it sounds like this is a simple GUI,
so squeaking a few milliseconds out of the time needed to press a button
doesn't really drive the design.

Good Luck,
---- Nick
 
A

Aaron

Nick,

Thanks alot.

Aaron

Nick Malik said:
Hi Aaron,

I'm not particularly fond of datagrids most of the time, although you could
make a case for it.

No. I'm thinking ahead, to what you didn't say:
If the user has the ability to add a row to a CSV file, and the user hates
Access, then the user may want to be able to:
1) view the contents of one or more existing rows
2) edit an existing row
3) delete an existing row

In this case, you don't have one row. You have a list or collection of
rows. each row has seven column values.

Given the small number of total rows, I'm assuming you could do this with a
simple arraylist collection where each element is a simple string array.
Not the most efficient structure, but it sounds like this is a simple GUI,
so squeaking a few milliseconds out of the time needed to press a button
doesn't really drive the design.

Good Luck,
---- Nick


all formulas changing application
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top