Q:

Out of memory error when adding rows to a datatable in vb.net

I am ultimately trying to use SqlBulkCopy to insert lines from a file into an SQL Server database. My code is below. 
The problem arises when I try to add too many rows to the data table. I'm not sure what the limit is, but my file has 5 million rows and I get an OutOfMemoryException on the line :
newRow.ItemArray = objFields.ToArray()

So I think it has to do with a limit on the data table.
Is there a way I can step through the addition of the rows and just insert some at a time or is there something I am doing wrong here that I can change?
The process does work fine with smaller files.
Thanks
Public Sub bulk_insert(file_name, delimiter, table_name)

        ' create a data table from the file
        ' ---------------------------------
        Dim lines = IO.File.ReadAllLines(file_name)
        Dim tbl = New DataTable
        Dim colCount = lines.First.Split(delimiter).Length

        ' add the correct number of columns to the table
        ' ----------------------------------------------
        For i As Int32 = 1 To colCount
            tbl.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
        Next

        ' add the lines to the data table
        ' -------------------------------
        For Each line In lines
            Dim objFields = From field In line.Split(delimiter) Select CType(field, Object)
            Dim newRow = tbl.Rows.Add()
            newRow.ItemArray = objFields.ToArray()
        Next

        ' create a connection to the database
        ' -----------------------------------
        Dim myConn = New SqlConnection(Main.this_page.sql_connection_string)

        ' start the Bulk Copy
        Dim SqlBulkCopy = New SqlBulkCopy(myConn)
        SqlBulkCopy.BatchSize = 10000
        SqlBulkCopy.BulkCopyTimeout = 10000

        ' open the connection
        ' -------------------
        myConn.Open()

        ' perform the copy
        ' ----------------
        SqlBulkCopy.DestinationTableName = table_name
        SqlBulkCopy.WriteToServer(tbl)

        ' cloe the connection
        ' -------------------
        myConn.Close()

    End Sub

Edit: after testing I see that when the error occurs, the row count is 2097152
Edit: since I don't want to answer my own question and I can't put this in a comment, I have included my result below based on the answer provided by the_lotus below. The code below solved the problem:
Public Sub bulk_insert(file_name, delimiter, table_name)

        ' create a connection to the database
        ' -----------------------------------
        Dim myConn = New SqlConnection(Main.this_page.sql_connection_string)
        ' open the connection
        ' -------------------
        myConn.Open()

        ' create a data table from the file
        ' ---------------------------------
        Dim lines = IO.File.ReadAllLines(file_name)
        Dim tbl = New DataTable
        Dim colCount = lines.First.Split(delimiter).Length

        ' add the correct number of columns to the table
        ' ----------------------------------------------
        For i As Int32 = 1 To colCount
            tbl.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
        Next

        Dim x As Integer = 0
        While x < lines.Count - 1

            ' clear the data table
            ' --------------------
            tbl.Clear()

            ' add the lines to the data table in 1M row increments
            ' ---------------------------------------------------
            If x + 1000000 > lines.Count - 1 Then
                For y = x To lines.Count - 1
                    Dim objFields = From field In lines(y).Split(delimiter) Select CType(field, Object)
                    Dim newRow = tbl.Rows.Add()
                    newRow.ItemArray = objFields.ToArray()
                Next
                x = lines.Count - 1
            Else
                For y = x To x + 1000000
                    Dim objFields = From field In lines(y).Split(delimiter) Select CType(field, Object)
                    Dim newRow = tbl.Rows.Add()
                    newRow.ItemArray = objFields.ToArray()
                Next
                x += 1000000
            End If

            ' start the Bulk Copy
            Dim SqlBulkCopy = New SqlBulkCopy(myConn)
            SqlBulkCopy.BatchSize = 1000000
            SqlBulkCopy.BulkCopyTimeout = 1000

            ' perform the copy
            ' ----------------
            SqlBulkCopy.DestinationTableName = table_name
            SqlBulkCopy.WriteToServer(tbl)

        End While

        ' cloe the connection
        ' -------------------
        myConn.Close()

    End Sub

A:

You are running out of ram... Your data is duplicated in lines and tbl. You already specify a BatchSize of 10000, you don't need to load more.
I would:

Read one line at a time
Add the information of that line in your tbl
When you have BatchSize amount of data in your tbl, send them to the database
Clear your tbl data and continue until file is done

If you have 5 million rows and each row has 100 character of data. That's 500mb of data just inside your lines variable. Then you duplicate this data in tbl, and DataTable must have extra information for each data point and row.

