I've run into a bit of an issue. One of my fields row {4} has a variable length of characters. However, its ending position in the file must be 23 characters from the end of row {3}. So I need to add however much white space is necessary to equal 23 characters for row {4}. As you can see, I've tried this with a for loop. Unfortunately it does not work. I'm not sure if it's the method I'm using, or the syntax that's the problem.
if ((checkResp91 != null) || (checkResp00 != null))
{
string selectCommandText91 = "SELECT * FROM EFT_BANK_INFORMATION1";
using (SqlDataAdapter adapter91 = new SqlDataAdapter(selectCommandText91, connection))
{
using (DataTable table91 = new DataTable("EFT_BANK_INFORMATION1"))
{
adapter91.Fill(table91);
System.Text.StringBuilder commaDelimitedText = new System.Text.StringBuilder();
//commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table91.Rows)
{
string spaceholder = "";
string spacer = row[4].ToString();
int countit = spacer.Length;
for (int i = countit; i < 23; i++)
{
spaceholder = spaceholder + " ";
}
string value = string.Format("{0}{1} {0} {2}{3}{4}{spaceholder}{5}", row[1], row[2], row[3], row[4], spaceholder, row[5], row[6]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
System.IO.File.WriteAllText("C:\\BillingExport\\tbl9_1_export.txt", commaDelimitedText.ToString());
}
}
}
Changed to
//commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table91.Rows)
{
string spacer = row[4].ToString();
string newvalue = (spacer + new string(' ', 23)).Substring(0, 23);
string value = string.Format("{0}{1} {0} {2}{3}{4}{5}", row[1], row[2], row[3], newvalue, row[5], row[6]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
System.IO.File.WriteAllText("C:\\BillingExport\\tbl9_1_export.txt", commaDelimitedText.ToString());
}
}
Which is giving me extra spaces for some reason, and putting them before the characters, when I need it after.
Output from the above code:
09 09 011301390011111119 AAAAAAA AAAABBBBBBBB BBBBBBBBB CO.
when it needs to be
09 09 011301390011111119AAAAAAA AAAA BBBBBBBB BBBBBBBBB CO.