Here is my problem...
I am validating rows length from a text file, one row at a time. If the length is <= 2033, it goes for processing. If it > 2033, it goes in a different file.
I would like to be able to add some information at the top of the error file. However, I want to add text only with the first row that will be added. So this is what I have so far:
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
if // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
swe.WriteLine(line);
count++;
}
else
{
swe.WriteLine(line);
count++;
}
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}
add a bool operator, and only do the extra line if it is first time, then set it so it does not write next time, but always write your line for the bad data
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
bool writeHeaderLine = false;
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
// only do this if it is false/first time
if(writeHeaderLine == false) // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
writeHeaderLine = true;
}
// always do this
swe.WriteLine(line);
count++;
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}