saving data from a query as a csv file
Asked Answered
P

1

5

I've got an application which opens a csv file and displays all the contents into a formatted datagridview. From there I have a button which opens another form that contains a series of checkboxes. The check boxes have all the attributes of the csv file we opened before, and the user is supposed to be able to query the file based on witch attributes they want, then save the file.

For example, if they only want a file which displays all the entries for animals with wings, they select the wings check box only. From there, you select the save button and it's supposed to save the file.

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    const string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
    const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
    StreamWriter writer = null;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {        
        filter = saveFileDialog1.FileName;
        writer = new StreamWriter(filter);

        writer.WriteLine(header);
        foreach (Animal animal in animalQuery)
        {
            writer.Write(animal);
        }  
        writer.Close();
    }
}

This is the code for the save button, but there are errors under:

filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter); 

I'm not sure why.

Pauwles answered 31/3, 2011 at 2:15 Comment(1)
The error/exceptions can be handy in this case. Also, where animalQuery is defined? The code seems incomplete. Also, even if wings is selected you are writing all the headers and I don't think in the correct position in the file.Glossematics
C
10

unless your code is exact, you cannot assign to a constant variable for your code saying:

filter = saveFileDialog1.FileName;

You declared "filter" as a constant variable further up:

const string filter = "CSV file (.csv)|.csv| All Files (.)|.";

Try that:

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
        saveFileDialog1.Filter = filter;
        const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
        StreamWriter writer = null;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filter = saveFileDialog1.FileName;
            writer = new StreamWriter(filter);

            writer.WriteLine(header);

            writer.Close();
        }

You use the SavefileDialog property "Filter" to define your list to filter by.

Colossus answered 31/3, 2011 at 3:5 Comment(2)
Ok awesome thanks! That errors gone, but now when I actually go to save it saves as simply a 'file' not an csv file, any ideas? I thought that would have been covered by string filter = "CSV file (.csv)|.csv| All Files (.)|."Pauwles
That will only filter what files show up in the SaveFileDialog. If someone types in a file name manually, or changes the filter to *.* and selects a non-csv file, the dialog will return that. I'd suggest validating that what's returned from the SaveFileDialog ends in .csv before continuing.Coppins

© 2022 - 2024 — McMap. All rights reserved.