I’ve VB.NET function that reads data from an Excel sheet and adds rows in a Datatable.
Private Function LoadDataToRows(ByVal TableName As DataTable, ByVal Header As System.Collections.Specialized.StringCollection) As Boolean
Dim HeaderDataExcel As String = String.Empty 'Data Header
For r As Integer = 1 To RangeDataArray.Rows.Count
Dim NewRow As DataRow = TableName.NewRow
For c As Integer = 1 To RangeDataArray.Columns.Count
If Not (IsNothing(DirectCast(ActiveSheetToManipulate.Cells.Item(r + DataStartRow, c), Excel.Range).Value)) Then
Dim ValueToLoad As String = TryCast(ActiveSheetToManipulate.Cells.Item(r + DataStartRow, c), Excel.Range).Value.ToString
HeaderDataExcel = TryCast(ActiveSheetToManipulate.Cells.Item(DataStartRow, c), Excel.Range).Value.ToString
Dim indice As Integer = Header.IndexOf(HeaderDataExcel)
TableName.NewRow(indice) = ValueToLoad
Else
'Todo
End If
Next
TableName.Rows.Add(NewRow)
Next
Return True
End Function
But no data was add to the table. Any suggestions?
I’m not a VB.NET programmer but you probably need to change the DataTable parameter to be passed ByRef rather than ByVal
In addition is the syntax for populating the NewRow columns correct? You should be using the created new data row instance rather than using TableName.NewRow?
I thought it was more like:
So your code would be something like the following (note I’ve changed the name of the new row variable because I think that’s causing part of your confusion)