when i try to read values from a .CVS-file i get sometimes a "System.IndexOutOfRangeException - Index was outside the bounds of the array" when a cell that represents an arrayindex is empty.
my code looks like this.
List<CustomCreatePrisInformationCommand> customCreatePrisList = new List<CustomCreatePrisInformationCommand>();
using (var stream = new StreamReader(command.File.InputStream))
{
int rowIndex = 0;
string line = stream.ReadLine();
string[] ColumnsHeaders = line.Split(new char[] { ';' },
StringSplitOptions.RemoveEmptyEntries);
line.TrimEnd(' ');
var header = new CustomCreatePrisInformationCommand();
header.Column1 = ColumnsHeaders[0];
header.Column2 = ColumnsHeaders[1];
header.Column3 = ColumnsHeaders[2];
header.Column4 = ColumnsHeaders[3];
header.Column5 = ColumnsHeaders[4];
header.Column6 = ColumnsHeaders[5];
header.Column7 = ColumnsHeaders[6];
header.Ar2018 = System.Convert.ToInt32(ColumnsHeaders[7]);
header.Ar2019 = System.Convert.ToInt32(ColumnsHeaders[8]);
header.Ar2020 = System.Convert.ToInt32(ColumnsHeaders[9]);
header.Ar2021 = System.Convert.ToInt32(ColumnsHeaders[10]);
header.Ar2022 = System.Convert.ToInt32(ColumnsHeaders[11]);
header.ProduktId = command.Produkt.Id;
header.AndratAv = command.AndratAv;
header.AndratDatum = DateTime.Now;
header.Pristyp = command.Produkt.Pristyp;
header.IsHeader = true;
customCreatePrisList.Add(header);
}
the error seems to occur when a cell is empty as the file has a fixed sets of columns up to 12 in total but it also can have less, and thats when the error occurs, i was thinking if there is any solution like .ifIsnullOrEmpty() for arrays to just skip empty values to the next value? or set a default value codewise if possible like a space maybe? or is there a better way to do this?