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!
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);
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);
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);
}
}
}