Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mikurski, </p> <p>I see one problem right off the bat. You are Dim'ing the Stream reader. You should enclose this in a using block, or be sure you do a <code>.Dispose</code> at the end of every code path. Anything that implements <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" rel="nofollow noreferrer">IDisposable</a> interface should be disposed or used in a using block as follows: </p> <pre><code> Using fs As New FileStream("C:\testing.txt", FileMode.Open) Using sr As StreamReader = New StreamReader(fs) Dim message As String = sr.ReadLine() MessageBox.Show(message) End Using End Using </code></pre> <p>I hope this helps, </p> <h2>Thanks!</h2> <p>EDIT: </p> <p>Here are the items that implement IDisposable that your code isn't disposing: </p> <ul> <li>DataTable</li> <li>StreamReader</li> </ul> <p>Hope this helps, </p> <h2>Thanks again!</h2> <p>Here is a function that is less memory intensive: </p> <pre><code>Function LoadFixedWidthFileToDataTable(ByVal filepath As String, ByRef Colnames() As String, ByRef colwidth() As Integer) As DataTable Dim filetable As New DataTable Try For Each name As String In Colnames filetable.Columns.Add(name) Next Try Using loadedfile As StreamReader = New StreamReader(filepath) Dim line As String = loadedfile.ReadLine Dim filerow As DataRow = filetable.NewRow Dim i As Integer = 0 While Not loadedfile.EndOfStream line = loadedfile.ReadLine filerow = filetable.NewRow i = 0 For Each colsize As Integer In colwidth Try filerow(i) = line.Substring(0, colsize) line = line.Remove(0, colsize) Catch ex As ArgumentOutOfRangeException Exit For End Try i = i + 1 Next filetable.Rows.Add(filerow) End While loadedfile.Close() End Using Catch io As IOException MsgBox(io.Message) Return Nothing End Try Return filetable Catch ex As Exception filetable.Dispose() End Try Return Nothing End Function </code></pre> <p>I notice that you're returning the thing that you should be disposing. If you're doing this, you cannot dispose of it here. Instead, the place this function is called from must dispose of it. If you wrap a using block around this you will have issues as the returned object will be disposed before you have a chance to act upon it from the calling code. </p> <p>Hope this helps, </p> <h2>Thanks again!</h2>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload