Reputation: 6500
I'm using the following code to write data from datatable to a text file. The text file is created,but its blank.
StreamWriter sw = File.CreateText("myfile.txt");
foreach (DataRow row in filteredResults.Rows)
{
sw.WriteLine(row["columnname"].ToString());
}
What am i doing wrong?
Upvotes: 0
Views: 80
Reputation: 1251
You need to use using around your code, like this:
using (StreamWriter sw = File.CreateText(@"..\..\myfile.txt"))
{
foreach (DataRow row in dt.Rows)
{
sw.WriteLine(row["QueueId"].ToString());
}
}
That should resolve your issue. Also to be absolutely sure that data has been send to the storage facility you could call Flush() method on your stream at the end of the using block.
Upvotes: 1
Reputation: 3634
you need use using statement OR sw.Flush()
and sw.Close()
..
using (StreamWriter sw = File.CreateText("myfile.txt"))
{
foreach (DataRow row in filteredResults.Rows)
{
sw.WriteLine(row["columnname"].ToString());
}
}
Upvotes: 0