-2

I need you to clear my point. I can use the application in SaveFileDialog stupid here but I'm a bit stuck. Basically, I have a OpenFileDialog to search .xml file and displays it in a datagridview. Then I will wish to export the contents of the datagridview to CSV files. And that is where it stops .... My SaveFileDialog is said but I know I have a writing worries but the beginning I did not understand why and how ...

I am entitled to this: 'Stream' does not contain a definition for 'WriteLine' and no extension method 'WriteLine' accepting a first argument of type 'Stream' was found (a using directive or reference to 'assembly-it is missing?)

When I put my comments in two lines myStream.writeline bah it tells me it is exported but empty CSV (normal what)

Can you help me please ?

private void SaveBtn_Click(object sender, EventArgs e)
{
    Stream myStream;         
    SaveFileDialog enregistrercsv = new SaveFileDialog();
    enregistrercsv.Filter = "Fichier CSV (*.csv)|*.csv|All files (*.*)|*.*";

    if (enregistrercsv.ShowDialog() == DialogResult.OK)
    {

        if ((myStream = enregistrercsv.OpenFile()) != null)
        {
            string strHeader = "";

                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {

                    strHeader += dataGridView1.Columns[i].HeaderText + ",";

                }

                    myStream.WriteLine(strHeader);



            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                    for (int n = 0; n < dataGridView1.Columns.Count; n++)
                    {

                        strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                    }

                    myStream.WriteLine(strRowValue);

            }

        }
        myStream.Close();

        MessageBox.Show("Fichier CSV créé avec succès FUCK YEAH");
    }

}

Thank you guys

2 Answers2

0

Try using StreamWriter instead of Stream, and it's generally best practices to only declare your Stream in as narrow scope as possible, and to do so in a using block, as so:

using(StreamWriter writer = enregistrercsv.OpenFile())
{
    if(writer != null)
    {
        //Perfrom your writer.WriteLine("value goes here"); here.
    }
}

This ensures that you close your stream as soon as you're done with it, so if you try to open a new stream at the same path it won't cause issues.

See if that fixes your issue.

Edit: Modified the code bit to have more context. The comment would be your two loops where you process and write information.

Ranger
  • 1,139
  • 1
  • 13
  • 34
  • Hello, ok but I don't know where to put in... sorry, i'm very new in C# and I have a headache haha. – Alessandro Apr 26 '16 at 16:01
  • not very useful comment @Alessandro, not to mention this is what the internet is used for.. there are plenty of working examples online you just need to let your fingers do the walking `new or not`..! – MethodMan Apr 26 '16 at 16:11
  • @MethodMan Why isn't my answer useful? He was trying to write to a generic stream that didn't have the WriteLine method. I directed him to a more specific object that would have WriteLine. – Ranger Apr 26 '16 at 16:14
  • 1
    @NexTerren I mean Alessandro sorry about that I made the edit – MethodMan Apr 26 '16 at 16:15
  • I'm on it since a few couple of hours, i'm getting crazy about this stuff. But thanks, i'll search again and again – Alessandro Apr 26 '16 at 16:17
  • @Alessandro I modified my answer to hopefully provide more context. The comment is where you'd perform your loops, processing of data, and WriteLine calls. – Ranger Apr 26 '16 at 16:18
  • 1
    @NexTerren, okay it's clear now and it's working, thank you very much :) Cheerz from Belgium – Alessandro Apr 26 '16 at 18:27
0

Here is an example using Streamwriter in a using block:

private void SaveBtn_Click(object sender, EventArgs e)
{
    SaveFileDialog enregistrercsv = new SaveFileDialog();
    enregistrercsv.Filter = "Fichier CSV (*.csv)|*.csv|All files (*.*)|*.*";

    if (enregistrercsv.ShowDialog() == DialogResult.OK)
    {

        using (StreamWriter writer = new StreamWriter(enregistrercsv.FileName)
        {
            string strHeader = "";

                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {

                    strHeader += dataGridView1.Columns[i].HeaderText + ",";

                }

                    writer.WriteLine(strHeader);

            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                    for (int n = 0; n < dataGridView1.Columns.Count; n++)
                    {

                        strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                    }


                    writer.WriteLine(strHeader);


            }

        }

        MessageBox.Show("Fichier CSV créé avec succès FUCK YEAH");
    }

}

Like Nex said, it is better to use StreamWriter and to declare it in as narrow a scope as possible.

Here's a discussion on the benefits of Streamwriter inside a using statement: https://stackoverflow.com/a/14637152/2844631

Community
  • 1
  • 1
iclosem
  • 15
  • 1
  • 3
  • Ok thank you iclosem, I understand why I have to use StreamWriter now :) But still not working, I have crazy stupids errors.. http://i.imgur.com/MHXzLWg.png Can't see why and makes me crazy. All seems ok :/ – Alessandro Apr 26 '16 at 18:19