Reputation: 65
I have a DataTable like this:
TerminalNo Payment ORNO Time
PHW0501 1,000.00 1 1:05pm
PHW0502 2,000.00 2 1:05pm
PHW0501 3,000.00 3 1:10pm
PHW0502 4,000.00 4 1:10pm
PHW0501 5,000.00 5 1:15pm
PHW0502 6,000.00 6 1:15pm
PHW0501 7,000.00 7 1:20pm
PHW0502 8,000.00 8 1:20pm
Everytime that payment has been made on both Terminal(at the same time or not), I want to get the Payment for both TerminalNo and write it on a different TXT file with different folder path.
Like this:
FolderPath1/Txt1
PHW0501, 1,000.00, 1:05pm, 1
FolderPath2/Txt2
PHW0502, 2,000.00, 1:05pm, 2
I only get this:
Folder1/Txt1 = PHW0501, 1,000.00, 1:05pm, 1
This is my code:
DataRow rw = dtTransaction.AsEnumerable().FirstOrDefault(tt => tt.Field<string>("TerminalNo") == "PHW0501");
DataRow rw2 = dtTransaction.AsEnumerable().FirstOrDefault(tt => tt.Field<string>("TerminalNo") == "PHW0502");
string fileName1 = GeneratedPath1 + DateTime.Now.ToString("MMddyyyy") + ".txt";
string fileName2 = GeneratedPath2 + DateTime.Now.ToString("MMddyyyy") + ".txt";
for (int i = 0; i < dtTransaction.Rows.Count; i++)
{
if (rw.ItemArray[4].ToString().Equals("PHW0501"))
{
if (dtTransaction.Rows[i]["TerminalNo"].ToString().Equals("PHW0501") && !dtTransaction.Rows[i]["ORNo"].ToString().Equals(""))
{
string Time = dtTransaction.Rows[i]["Time"];
double Payment = -double.Parse(dtTransaction.Rows[i]["Payment"].ToString());
int ORNo = dtTransaction.Rows[i]["ORNo"].ToString();
using (StreamWriter sw = File.AppendText(fileName1))
{
sw.Write("PHW0501" + ",");
sw.Write(Payment + ",");
sw.Write(Time + ",");
sw.Write(ORNo);
}
}
}
if (rw2.ItemArray[4].ToString().Equals("PHW0502"))
{
if (dtTransaction.Rows[i]["TerminalNo"].ToString().Equals("PHW0502") && !dtTransaction.Rows[i]["ORNo"].ToString().Equals(""))
{
string Time = dtTransaction.Rows[i]["Time"];
double Payment = double.Parse(dtTransaction.Rows[i]["Payment"].ToString());
int ORNo = dtTransaction.Rows[i]["ORNo"].ToString();
using (StreamWriter sw = File.AppendText(fileName2))
{
sw.Write("PHW0502" + ",");
sw.Write(Payment + ",");
sw.Write(Time + ",");
sw.Write(ORNo);
}
}
}
}
Upvotes: 0
Views: 73
Reputation: 14251
Use LINQ for grouping.
var grouped = dtTransaction.AsEnumerable()
.Where(row => row["ORNo"].ToString() != "")
.GroupBy(row => row["TerminalNo"]);
foreach (var group in grouped)
{
string folder = group.Key.ToString();
Directory.CreateDirectory(folder);
string filename = DateTime.UtcNow.ToString("MMddyyyy") + ".txt";
string path = Path.Combine(folder, filename);
using (StreamWriter writer = File.AppendText(path))
{
foreach (DataRow row in group)
{
writer.WriteLine(string.Join(",", row.ItemArray));
}
}
}
Here directories named PHW0501 and PHW0502 will be created. You can add some prefix to them.
Next, in each directory will be created file.
Comma-separated data is entered to the file.
Upvotes: 1
Reputation: 132
use this code. You have to put after filePath "true" to continue second line.
string path = "PATH TO YOU TEXT FILE WITH FILENAME AND EXTENSION";
using (StreamWriter writetext = new StreamWriter(path, true))
{
writetext.WriteLine("Language: " + language);
writetext.WriteLine("Log Date: " + DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo));
}
Upvotes: 0