I have tried to pass the data from a .csv file to a datatable and the code seems to be all ok but I have been back from this error for quite some time and I do not know how to resolve it.
I have two columns in my .csv file and a string type another integer type and the error by what I realized is to give in the first line in which the "ATID" is not integer in that first line being that the rest is a whole number and I do not know how to solve thanked help. I've already tried converting and it did not work (I may have gone wrong, some help is welcome).
Example:
PPID; ATID
asd; 1
asd; 2
asd; 3
dsa; 4
erf; 5
Error: System.ArgumentException: 'Input string was not in a correct format.Couldn't store < # assetId # > in AtId Column. Expected type is Int32. '
Code:
using (StreamReader sr = new
StreamReader(@"C:Pathfile....csv"))
{
var datatable = new DataTable();
datatable.Columns.Add("PPId", typeof(string));
datatable.Columns.Add("AtId", typeof(int));
string line;
while ((line = sr.ReadLine()) != null)
{
System.Diagnostics.Debug.WriteLine(line + "\n");
string[] lineitems = line.Split(";");
DataRow dr = datatable.NewRow();
dr["PPId"] = lineitems[0];
dr["AtId"] = lineitems[1];
datatable.Rows.Add(dr);
}
}