T
tshad
I have an excel file that has 3 columns.
Reading this into a DataTable
I am reading the xls file into the DataTable, like so:
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" +
AppSettings.InputFilePath + Path.GetFileName(strFile) + ";Extended
Properties=\"Excel 8.0;HDR=No;IMEX=1\"");
connection.Open();
tableName =
((DataTable)connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null)).Rows[0].ItemArray[2].ToString();
da = new OleDbDataAdapter("SELECT * FROM " + tableName, connection);
da.Fill(ds);
And the columns look something like this:
COUNT KEY AMT
11 0A102 $38.00
23 0A111 $150.88
This works fine and ends up with 3 columns in my DataTable, as it should.
But if I Cut the 2nd column and paste it before count so that the file looks
like:
KEY COUNT AMT
0A102 11 $38.00
0A111 23 $150.88
When reading the xls file, the datatable now shows that there are only 2
columns (dt.Columns.Count = 2)
When looping through the collection of rows I get:
dr[0] = "COUNT"
dr[1] = "AMT"
dr[2] - 'dr[2]' threw an exception of type
'System.IndexOutOfRangeException'
Reading this into a DataTable
I am reading the xls file into the DataTable, like so:
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" +
AppSettings.InputFilePath + Path.GetFileName(strFile) + ";Extended
Properties=\"Excel 8.0;HDR=No;IMEX=1\"");
connection.Open();
tableName =
((DataTable)connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null)).Rows[0].ItemArray[2].ToString();
da = new OleDbDataAdapter("SELECT * FROM " + tableName, connection);
da.Fill(ds);
And the columns look something like this:
COUNT KEY AMT
11 0A102 $38.00
23 0A111 $150.88
This works fine and ends up with 3 columns in my DataTable, as it should.
But if I Cut the 2nd column and paste it before count so that the file looks
like:
KEY COUNT AMT
0A102 11 $38.00
0A111 23 $150.88
When reading the xls file, the datatable now shows that there are only 2
columns (dt.Columns.Count = 2)
When looping through the collection of rows I get:
dr[0] = "COUNT"
dr[1] = "AMT"
dr[2] - 'dr[2]' threw an exception of type
'System.IndexOutOfRangeException'