Reputation: 29
I'm stumped. I have a data from 3 datatables being written to a text file but when i'm writing a certain table to the file it's writing duplicates and I can't figure out why.
Here's the code for writing to the file
public void writeCharge2(StreamWriter sw, string dos1, int i)
{
getCharges(dos1, SqlCon);
foreach (DataRow row in chargeTable.Rows)
{
for (int n = 0; n <= chargeTable.Rows.Count -1; n++)
{
ChargeLine = chargeTable.Rows[n][0].ToString();
writeCharge(sw, dos1, i, ChargeLine);
sw.WriteLine();
payChart.Reset();
payGraph.Reset();
}
}
chargeTable.Reset();
}
public void writeCharge(StreamWriter sw, string dos2, int i, string CPTLine)
{
getPayments(ChargeLine, SqlCon, dos2);
string description = getDesc(CPTLine, SqlCon);
sw.WriteLine(" Description: " + CPTLine + " - " + description);
sw.WriteLine("-----------Payments-----------");
if (i < payGraph.Rows.Count)
{
foreach (DataRow row in payGraph.Rows)
{
for (i = 0; i < row.ItemArray.Length -1; i++)
{
DateTime payDate = Convert.ToDateTime(payGraph.Rows[i][0].ToString());
string payDate1 = payDate.ToString("MM/dd/yyyy");
decimal money = Convert.ToDecimal(payGraph.Rows[i][1].ToString());
string money1 = money.ToString("C");
string payer = payGraph.Rows[i][2].ToString();
sw.Write("Payment Date: " + payDate1 + " | ");
sw.Write("Amount: " + money1 + " | ");
sw.Write("Payer: " + payer);
sw.WriteLine();
}
sw.WriteLine();
}
}
}
public void writeData(StreamWriter sw)
{
clearTables();
getDos(SqlCon);
int i = 0;
foreach(DataRow row in servicetable.Rows)
{
string dosNow = Convert.ToString(servicetable.Rows[i][0]);
setupPat(sw, i);
writeCharge2(sw, dosNow, i);
sw.WriteLine(Environment.NewLine);
i++;
}
Here is how it's being written to the file which you can see the duplicate lines being written
Date of Service: 08/22/2012
Description: 76775 - US RETROPERITONEAL LIMITED
-----------Payments-----------
Payment Date: 09/10/2012 | Amount: $23.14 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $5.78 | Payer: Insurance
Payment Date: 09/10/2012 | Amount: $23.14 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $5.78 | Payer: Insurance
Description: 93922 - EXTREMITY NON-INVASIVE 1 OR 2 LEVELS
-----------Payments-----------
Payment Date: 09/10/2012 | Amount: $9.74 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $2.43 | Payer: Insurance
Payment Date: 09/10/2012 | Amount: $9.74 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $2.43 | Payer: Insurance
Description: 76775 - US RETROPERITONEAL LIMITED
-----------Payments-----------
Payment Date: 09/10/2012 | Amount: $23.14 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $5.78 | Payer: Insurance
Payment Date: 09/10/2012 | Amount: $23.14 | Payer: Insurance
Payment Date: 09/24/2012 | Amount: $5.78 | Payer: Insurance
Upvotes: 1
Views: 970
Reputation: 1531
This is because foreach
loop is not necessary within the writeCharge2 function. You iterate rows using foreach
and then in each iteration print all the rows, so for 2 rows they will be printed twice, for 3 rows - 3 times etc.
Upvotes: 1