views:

57

answers:

1

I am having some trouble with my program logic that loops through a collection of data that exists in two separate ListViews. After looping though and extracting the data from the ListView, I then add everything into a comma delimited text file (CLOSEULDCONFIG.TXT).

The first time that I execute this logic, everything works as it should. If I execute this logic again, I get 2 copies of that which is in the ListView. Each time I run through this logic, number of copies of the previously added ListView items is increased by 1.

This is undesirable as I would like to add the same number of elements as that which I have in my ListView to my text file. Can anyone spot what is wrong with my nested foreach statements that is causing this?

                        // HAZMAT PACKAGE ERROR LISTVIEW ITEMS               
                        foreach (ListViewItem HazPackErrItems in HazmatPackageErrorListview.Items)
                        {
                            bool first = true;
                            foreach (ListViewItem.ListViewSubItem HazPackErrSub in HazPackErrItems.SubItems)
                            { 
                                // removes the first element of each comma delimited string
                                if (first)
                                    first = false;
                                else
                                    CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig += " " + HazPackErrSub.Text + ",";
                            }
                        } 

                        // HAZMAT WEIGHT AND COUNT COLLECTED LISTVIEW ITEMS
                        foreach (ListViewItem HazWeightAndCountItems in HazmatWeightAndCountListview.Items)
                        {
                            bool first = true;
                            foreach (ListViewItem.ListViewSubItem HazWeightAndCountSub in HazWeightAndCountItems.SubItems)
                            {
                               // removes the first element of each comma delimited string
                                if (first)
                                    first = false;
                                else
                                    CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig += " " + HazWeightAndCountSub.Text + ",";
                            }
                        }

                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter("CLOSEULDCONFIG.TXT", true))
                        {
                            if (!AlreadyExists)
                            {
                                sw.WriteLine(PresetNameConfig +
                                CloseULDSubmitLogDataRequestCloseULDConfig +
                                CloseULDSubmitLogDataResponseCloseULDConfig +
                                CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig +
                                CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig +
                                CloseULDDateTimeConfig);
                            }
                        }
+1  A: 

If I'm not mistaken, you are opening the file to append, instead of overwrite. Have you checked the file itself to see if the data is being duplicated there?

hmcclungiii