You need to specify the data type of each column and check data against that.
This is one way of many for how you can do it. I hope it can help you on the way.
dt.Columns.Add(t, typeof(int));
dt.Columns.Add(t, typeof(double));
etc.
As you are doing this in a loop you need an array with the matching data types or do it manually inside the loop.
for (int i=0; i<colname.length;>{
if (i == 0)
dt.Columns.Add(colname[i], typeof(int));
else if (i == 1)
dt.Columns.Add(colname[i], typeof(double));
}
Not the most elegant way and if the number of columns change or their types, then you have to change the code.
The best option is to get the data type from the document, but without any knowledge of the structure of the CSV file I cannot suggest anything else.
The point is that if you have set the data type of each column you can use it to validate the data.
int line = 0;
int col = 0;
try
{
for (line = 1; line < tempLines.Length; line++)
{
string[] columns = tempLines[line].Split(',')
for (col=0; col < columns.Length; col++)
{
object val = Convert.ChangeType(columns[col], dt.Columns[col].DataType);
dt.Rows.Add(val);
}
}
}
catch (Exception ex);
{
throw new Exception(String.Format("Error at line {0}, col {1}, {2}", line, col, ex.Message);
}