-2

I will create a CSV Import for invoices for my mother, so I have create a Interface that read a CSV file, and for each line in the CSV the values to a List<List<string>>. Now is the Problem the following:

ABC; 1300; Customer 1; Product1; Product Description; 0; 0; 0
ABC; 1300; Customer 1; Product2; Product Description; 1; 2; 0

The Second value is the invoice number and the 4-5 values are for the invoice "lines". I have really no Idea how to solve this that all line with the same value as second go though the same file. Maybe you can help ?

kb_
  • 620
  • 4
  • 21
  • Could you specify which is `second value` and which are `4-5 values`? – qxg Oct 27 '15 at 07:00
  • what you try???? why you need List of Lists? – AsfK Oct 27 '15 at 07:01
  • What exacly is the problem here? Id it reading the CSV file that is the problem? maybe you have some code you can include? http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array – Teis Lindemark Oct 27 '15 at 07:02
  • The second value is the invoice number (unique number), the 4-5 values are the procuts which should show on the invoice – kb_ Oct 27 '15 at 07:02
  • 1
    @invidicult: The second value is A) not a unique number and B) is before the first comma. This question is all wrong and makes very little sense. You need to go back and figure out what your question is. – Jonathan Wood Oct 27 '15 at 07:03
  • @Jonathan Wood: I've changed the sample. The second value is unique for each order, not for each line – kb_ Oct 27 '15 at 07:05
  • @invidicult: What the heck am I missing. The second value is **not** unique. And is this a CSV file, or a semicolon delimited file? And when you figure that out, what issue are you trying to solve? – Jonathan Wood Oct 27 '15 at 07:07
  • While you're re-thinking the question, some suggestions: the [`TextFieldParser`](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser%28v=vs.110%29.aspx) can read a CSV file, and [grouping](https://msdn.microsoft.com/en-us/library/bb545971.aspx) can merge multiple results with a common property. – dbc Oct 27 '15 at 07:07
  • @JonathanWood - CSV files are often delimited by a semicolon when a comma is used for a decimal separator (in e.g. European countries). It's [`CurrentCulture.TextInfo.ListSeparator`](http://stackoverflow.com/questions/6834534/). – dbc Oct 27 '15 at 07:10
  • 1
    @dbc: Then he should probably clarify what sort of delimiters he's using, since CSV stands for comma-delimited values, and his original question used a mix of commas and semicolons. – Jonathan Wood Oct 27 '15 at 07:16

1 Answers1

1

follow this answer, it's should solve your problem

class invoice
{
    public String field0, field2, field3, field4;
    public int field1, field5, field6, field7;
}

private void Form1_Load(object sender, EventArgs e)
{
    List<invoice> invoiceList = new List<invoice>();
    var reader = new StreamReader(File.OpenRead("invoice.csv"));
    while (!reader.EndOfStream)
    {
        String line = reader.ReadLine();
        String[] values = line.Split(';');

        invoice inv = new invoice();
        inv.field0 = values[0];
        inv.field1 = int.Parse(values[1]);
        inv.field2 = values[2];
        inv.field3 = values[3];
        inv.field4 = values[4];
        inv.field5 = int.Parse(values[5]);
        inv.field6 = int.Parse(values[6]);
        inv.field7 = int.Parse(values[7]);
        invoiceList.Add(inv);
    }
    reader.Close();
}

just change variables names according your title and next time, please do HW :) to tell the truth, I answer only because you help your mom

btw, fields in CSV file are separated by , and not ; (CSV = Comma Separated Values), see this.

AsfK
  • 3,328
  • 4
  • 36
  • 73