-1

So basicilly I wanna save the list datas to csv.But the only thing is saved in the file is: System.Collections.Generic.List`1[WindowsFormsApp1.Autok],I dont know excatly how to fix,the writer is screwd up

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        List<Autok> aLista = new List<Autok>();
        Autok a = new Autok();
        public Form1()
        {
            InitializeComponent();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string NewGUID = System.Guid.NewGuid().ToString();
            tbTermekkod.Text = NewGUID.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            a.Termekkod = tbTermekkod.Text;
            a.Gyarto = tbGyarto.Text;
            a.Tipus = tbTipus.Text;
            a.Szin = tbSzin.Text;
            a.Felszereltsegiszint = tbFelsz.Text;
            a.Ar = Convert.ToInt32(tbAr.Text);
            aLista.Add(a);


        }

        private void button3_Click(object sender, EventArgs e)
        {

        using (StreamWriter sW = new StreamWriter(@"C:\Users\Tamás\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\rainbow.csv", false, Encoding.Default))
        {
            foreach (Autok item in aLista)
            {
                sW.WriteLine(a);
                sW.Close();
            }

        }

        }
    }
}
  • 1
    First, you are entering the same object a again and again. You probably want to create a new one each time. Second, you probably want `sW.WriteLine(a);` instead of `sW.WriteLine(aLista);` – Klaus Gütter Nov 25 '18 at 13:54
  • I changed it now I getting this WindowsFormsApp1.Autok – Tamás Varga Nov 25 '18 at 14:06
  • 1
    Third, you probably want to override `ToString()` of `Autok`. Fourth, you probably want to remove the line `sW.Close();` – Dennis_E Nov 25 '18 at 14:14
  • Possible duplicate of [Override .ToString method c#](https://stackoverflow.com/questions/18200427/override-tostring-method-c-sharp) – mjwills Nov 26 '18 at 00:39

3 Answers3

0

As Klaus Gütter said you have to create a new instance of Autok each time. And to save it correctly I supose you have to override the ToString Method of Autok.

0

In the button3_Click click event you are writing the global variable "a" to your file instead of the the iterator "item". If you change "a" to "item" you still will not get the expected results. As Klaus said you could override the ToString() method of your Autok object, but I think a better idea would be to use a CSV library (I highly recommned CsvHelper). Then you could change your code to:

private void button3_Click(object sender, EventArgs e)
{
    using (StreamWriter sW = new StreamWriter(@"C:\Users\Tamás\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\rainbow.csv", false, Encoding.Default)
    {
        var csvWriter = new CsvWriter( sW);
        csvWriter.WriteRecords( aLista);
    }
}

If you don't use CsvHelper you could override the ToString()and change it to this:

private void button3_Click(object sender, EventArgs e)
{
    using (StreamWriter sW = new StreamWriter(@"C:\Users\Tamás\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\rainbow.csv", false, Encoding.Default))
    {
       foreach (Autok item in aLista)
       {
            sW.WriteLine(item.ToString());
            sW.Close();
       }
    }
}


public override string ToString()
{
    var comma = ",";
    return this.Termekkod + comma + this.Gyarto + comma + etc...
}
Kevin
  • 2,566
  • 1
  • 11
  • 12
  • CsvHelper is not option , but I did the override and the foreach all data is written in one cell.How do I split? – Tamás Varga Nov 25 '18 at 14:53
  • See this article if you need to escape things https://stackoverflow.com/questions/4685705/good-csv-writer-for-c see my update for the ToString() method. – Kevin Nov 26 '18 at 00:31
0

try this

 foreach (Autok item in aLista)
            {
           sW.WriteLine(item.Termekkod +
                        item.Gyarto+ 
                        item.Tipus+ 
                        item.Szin+
                        item.Felszereltsegiszint + 
                        item.Ar+"");

                sW.Close();
            }