As soon as the the program code checks the first column that's invalid in the excel file, it doesn't continue to check the rest. Is there a way the loop still continues and checks for the rest of the columns?
ValidationFile Code is given below. It goes through Excel Column names and checks if column size is not more than 128 chracters long and is not empty or the whole as a whole is not empty.
public Boolean InvalidColumnNames()
{
using (ReadAndStreamFile())
{
reader.Read();
{
int counter = 0;
var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
if (ColumnsNames.Count != 0 && reader.Read() == true)
{
for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
{
var excel = new ExcelDataReaderFile();
var cellAddress = excel.GetAddress(counter, 0);
counter += 1;
if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
{
Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
return true;
}
else if (ColumnsNames[columnNumber] == null)
{
Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
return true;
}
else
{
// return true;
}
continue;
}
}
else
{
Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
return true;
};
}
reader.Dispose();
reader.Close();
return false;
}
}
Below is the Program Main method which describes if Invalid column names is true then give a message validation for such and such file failed otherwise it has passed the validation.
class program
{
public static void Main(string[] args)
{
string path = ConfigurationManager.AppSettings["path"].ToString();
string search = ConfigurationManager.AppSettings["search"].ToString();
ExcelDataReaderFile ExcelDataReaderFile = new ExcelDataReaderFile();
string[] fileEntries = Directory.GetFiles(path, "*" + search + "*", SearchOption.AllDirectories);
dynamic counter = 0;
BasicValidation BasicValidation = new BasicValidation();
foreach (string file in fileEntries)
{
BasicValidation basicValidation = new BasicValidation(file, 128, 32767, 128, 128);
counter += 1;
Logger.Info($"Validating {basicValidation.GetFileName(file)}");
//Console.ForegroundColor = ConsoleColor.Blue;
if (basicValidation.InvalidColumnNames())
{
//if the above statments are true then run those methods and log an error
//Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {Regex.Unescape(path)} has failed validation. Check the error log for details.");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
else
{
Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {file} has passed validation checks.");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
else
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
if you write return
statement in any for/foreach loop, loop terminates executing and it returns from there.
So use variable and assign it value based on condition and once loop completes return
from the method.
public Boolean InvalidColumnNames()
{
bool errorInFile = false;
using (ReadAndStreamFile())
{
reader.Read();
{
int counter = 0;
var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
if (ColumnsNames.Count != 0 && reader.Read() == true)
{
for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
{
var excel = new ExcelDataReaderFile();
var cellAddress = excel.GetAddress(counter, 0);
counter += 1;
if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
{
Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
errorInFile = true;
}
else if (ColumnsNames[columnNumber] == null)
{
Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
errorInFile = true;
}
else
{
// return true;
}
}
}
else
{
Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
errorInFile = true;
};
}
reader.Dispose();
reader.Close();
return errorInFile;
}
}