Solved

VB 2008, reading text file

Posted on 2010-11-29
7
755 Views
Last Modified: 2012-06-27
I have the following code to read data from a txt file. This code, although it works, may be using vb 6 version.
 
        Dim ScoreFile As StreamReader
        ScoreFile = File.OpenText("ScoreList.txt")
        Dim strTemp As String
        Dim fileline As Object

        strTemp = ScoreFile.ReadLine()

        Do Until ScoreFile.Peek = -1
            strTemp = ScoreFile.ReadLine()'<-- to bypass the header
            fileline = strTemp.Split("|")

            For i = 0 To 5
                Debug.Print(fileline(i))
            Next
        Loop

Question: What this code using VB 2008 version?

Thank you
StudentName|Score1|Score2|Score3|Score4|Score5|NoOfScores
a,a        |1     |      |      |      |      |1       
b,b        |22    |2     |      |      |      |2
c,c        |3     |3     |      |      |      |2
d,d        |4     |4     |4     |4     |      |4
e,e        |5     |5     |5     |5     |5     |5
f,f        |      |      |      |      |      |0

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 4
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34236523
It's not much different...
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Values() As String
        Using ScoreFile As New StreamReader("ScoreList.txt")
            ScoreFile.ReadLine() ' skip the header
            While ScoreFile.Peek <> 0
                Values = ScoreFile.ReadLine.Split("|")
                For i As Integer = 0 To Values.GetUpperBound(0)
                    Debug.Print(Values(i))
                Next
            End While
        End Using
    End Sub

End Class

Open in new window

0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 34236541
Thank you
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34236661
I have the following error. Unlike 99% of the time, your code causes an error as shown on the attached image.
VB2008TxtFileReadErr.png
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34240470
Hmm...try this instead:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim line As String
        Dim Values() As String
        Using ScoreFile As New StreamReader("ScoreList.txt")
            ScoreFile.ReadLine() ' skip the header

            If ScoreFile.Peek <> 0 Then
                line = ScoreFile.ReadLine
                While Not IsNothing(line)
                    Values = line.Split("|")
                    For i As Integer = 0 To Values.GetUpperBound(0)
                        Debug.Print(Values(i))
                    Next

                    line = ScoreFile.ReadLine
                End While
            End If
        End Using
    End Sub

End Class

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34240476
If that doesn't fix it can you upload a sample text file?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34240563
I will test it now.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34241899
Thanks, it works.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Exceptions 3 46
C# Object Reference is Required for the non-static field, method, or property 10 34
VB.NET 2008 Publish Error 2 27
Expression Evaluater 3 25
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question