Reputation: 123
I'm trying to return all data from a list that has more than 30 items. For some reason my code is only giving me the list's last item. I'm sure this is a simple fix but if anyone could help me out that would be great. Here's the code.
List<string> propnumList = new List<string>();
foreach (DataRow drRow in ds7.Tables[0].Rows)
{
for (int i = 0; i < ds7.Tables[0].Columns.Count; i++)
{
propnumList.Add(drRow[i].ToString());
}
}
using (StreamWriter sw = new StreamWriter("propnumList.txt"))
{
foreach (string s in propnumList)
{
sw.WriteLine(s);
}
}
string tempProp = "";
foreach (string x in propnumList)
{
if (x.Length < 30)
{
x.Equals(null);
}
else
{
tempProp = x.Substring(31);
using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
write.WriteLine(tempProp); WANT TO RETURN MORE THAN JUST LAST ITEM
}
}
}
Upvotes: 1
Views: 1524
Reputation: 12654
You are overwriting your output file on each iteration. Place file opening outside the foreach.
using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
foreach (string x in propnumList)
{
....
}
}
Upvotes: 15