Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a logic issue that seems so easy, but am having trouble trying figure it out.

I am writing to a .txt file using a StreamWriter and that is working fine as it should. The thing is, is I am looping through the rows and need to write a "9" after the looping, but the way it is set up, a "9" gets printed for every row.

How can I write the "9" to the file after all the rows are written? Here is the code. Thank you!

C#
foreach (DataRow dr in dt.Rows)
                        {
                            if (dr[12].ToString().Contains("55|"))
                            {
                                string file55 = "C:\\authorize.netTEST\\55-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                                StreamWriter sw = new StreamWriter(file55, true);//inetpub\temp
                                if (dr[0].ToString() != "2")
                                {
                                    if (dr[9].ToString() != "Total Amount")
                                    {
                                        sw.Write("6                            ");
                                        sw.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                        sw.Write((dr[12].ToString().Replace("55|","")).PadRight(15, ' '));
                                        sw.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                        sw.Write(sw.NewLine);
                                    }
                                }

                                    sw.Write("9");
                                    sw.Write(sw.NewLine);

                                sw.Close();

                                Session.Add("DatedFileName", file55);
                            }if (dr[12].ToString().Contains("99|"))
                                {
                                string file99 = "C:\\authorize.netTEST\\99-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                                StreamWriter sw = new StreamWriter(file99, true);//inetpub\temp
                                if (dr[0].ToString() != "2")
                                {
                                    if (dr[9].ToString() != "Total Amount")
                                    {
                                        sw.Write("6                            ");
                                        sw.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                        sw.Write((dr[12].ToString().Replace("99|", "")).PadRight(15, ' '));
                                        sw.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                        sw.Write(sw.NewLine);
                                    }
                                }
                                    sw.Write("9");
                                    sw.Write(sw.NewLine);
                                 sw.Close();
                                Session.Add("DatedFileName2", file99);
                            }
                         }
                }
Posted
Updated 14-Jun-12 3:44am
v2

1 solution

I figured it out. I just used two foreach loops, one for each file needing to be created. I thought I had tried that before but must not have done it properly.

I would still appreciate any advice or suggestions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900