0

So I've been trying to figure out how to bring an entire line of a .csv file but only the ones who's first string matches another one. This is what I got so far, all im getting back in my listbox is info from the same random line. If you guys can help me with the logic it would help out a lot thanks

cbocustinfo.Items.Clear();
lstcustinfo.Items.Clear();
StreamReader infile, transdata;
infile = File.OpenText(@"E:\AS2customers.csv");
transdata= File.OpenText(@"E:\AS2data.csv");
string[] custinfo, names;
string[] custtrans;
do
{
    custtrans = transdata.ReadLine().Split(',');
    if (custinfo[1] == custtrans[0])
    {
        lstcustinfo.Items.Add(custtrans[3] + " " + custtrans[4]);
    }
}
while (transdata.EndOfStream != True);
infile.Close(); 
transdata.Close();

Here is where I initialize custinfo

do
{ 
    custinfo = infile.ReadLine().Split(',');
    names = custinfo[0].Split(' ');
    cbocustinfo.Items.Add(names[0] +" "+ names[1]+ " " + custinfo[1]);
}
while (infile.EndOfStream != true);
JoriO
  • 1,050
  • 6
  • 13
  • In my head, this generates a runtime error: `if (custinfo[1]` because you never initialize the custinfo array. – Rufus L Nov 03 '14 at 04:14
  • Also, your streamReaders should be in using statements, since they implement IDisposable() – Rufus L Nov 03 '14 at 04:18
  • When do you populate data into the `custinfo` array? Currently it never appears to be initialized. – grovesNL Nov 03 '14 at 04:20
  • i put the using as using Systems.IO; before i start programming I initialize custinfo earlier i just didnt want to include it cause it might be more confusing too long – Marhugo Chiccflores Nov 03 '14 at 04:28
  • Well, in that `do` loop you just added, you are overwriting `custinfo` and `names` over and over again until you get to the end of `infile`. – Rufus L Nov 03 '14 at 04:37

1 Answers1

0

If I understand what you're trying to do correctly, maybe it would be easier to just read the files into two strings, then do the splitting and looping over those. I don't know your file formats, so this may be doing unnecessary processing (looping through all the transactions for every customer).

For example:

cbocustinfo.Items.Clear();
lstcustinfo.Items.Clear();

var customers = File.ReadAllText(@"E:\AS2customers.csv")
    .Split(new []{Environment.NewLine}, StringSplitOptions.None);

var transactions = File.ReadAllText(@"E:\AS2data.csv")
    .Split(new []{Environment.NewLine}, StringSplitOptions.None);

foreach (var customer in customers)
{
    var custInfo = customer.Split(',');
    var names = custInfo[0].Split(' ');
    cbocustinfo.Items.Add(names[0] + " " + names[1]+ " " + custinfo[1]);

    foreach (var transaction in transactions)
    {
        var transInfo = transaction.Split(',');

        if (custInfo[1] == transInfo[0])
        {
            lstcustinfo.Items.Add(transInfo[3] + " " + transInfo[4]);
        }
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • i don't have much experience with foreach where do you initialize the customer variable? or does it just represent individual lines in the file? – Marhugo Chiccflores Nov 03 '14 at 05:03
  • it's initialized here: `foreach (var customer in customers)`. var is a shortcut form which can be used when the type can be inferred at design-time. in this case, it's a string. In this case, it's the same as writing `foreach (string customer in customers)`. – Rufus L Nov 03 '14 at 05:07
  • ok yeah that makes sense it really helps me out in understanding, thanks. i try to use the method you presented but it give me an out of bounds of array error for every instance of custinfo[1] – Marhugo Chiccflores Nov 03 '14 at 05:19
  • i dont know if it matters but im doing all this on a selectedindex event from a combobox – Marhugo Chiccflores Nov 03 '14 at 05:20