For several years I have been using the following code without issue:
Dim dtDataTable As DataTable = New DataTable Dim iRow As Integer = 0 Dim iCol As Integer = 0 Dim dtCol() As DataColumn = Nothing Dim oRow As DataRow = Nothing For iLoop As Integer = 1 To 3 dtCol(iLoop - 1) = New DataColumn dtDataTable.Columns.Add(dtCol(iLoop - 1)) Next iLoop
For some reason, however, today I encountered a Null Reference exception on the line:
"dtCol(iLoop - 1) = New DataColumn"
Earlier in the code I have identified dtCol() as a DataColumn and assigned Nothing to it until I needed to. This is really weird.
I am now using VisualStudio Community 2015 with .Net 4.6. Am I mussing something. I bet ots going to be really easy, but I can't see it for some reason. Your help would be appreciated. Thank you
Visual Basic.NET
Last Comment
Fernando Soto
8/22/2022 - Mon
Scott McDaniel (EE MVE )
You can add Columns like this:
For iLoop As Integer = 1 To 3
dtDataTable.Columns.Add ("Col" & (iLoop-1).ToString, GetType(String))
Next iLoop
For iLoop As Integer = 1 To 3
dtDataTable.Columns.Add ("Col" & (iLoop-1).ToString, GetType(String))
Next iLoop