VS 2013 Skips First Line when reading data from text file-VBForums
Results 1 to 12 of 12

Thread: Skips First Line when reading data from text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    228

    Skips First Line when reading data from text file

    Hi everyone,

    Here is my code & text file, when i run this program, every time is skips first line from text file, rest everything is working perfectly.
    Please help me what is the issue & how to resolve it.

    Thanks
    Ladak

    Code:
            Dim i As Long = 0
            Dim sr As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Dim line As String = sr.ReadLine()
            Dim ItemIDNumber As String = ""
            Dim AddNewRow As Boolean = True
            Dim Msg1 As Object, Msg2 As Object
            Dim Qty As Object
            Dim Total As Object
    
    
    
            Do Until (sr.EndOfStream)
    
                line = sr.ReadLine()
                Dim fields() As String = line.Split(",")
                '
                MessageBox.Show(fields(0))
                MessageBox.Show(fields(1))
                '
          Loop



    037361006754,55
    085715311054,45
    156079395063,22
    616919104564,20
    718088348296,30
    741655991472,33
    793379301099,44
    881884492064,42
    156079395063,22
    793379301099,44
    085715311054,45
    156079395063,22
    085715311054,45
    156079395063,22
    793379301099,44
    085715311054,45
    085715311054,45
    793379301099,44
    793379301099,44
    793379301099,44

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    Re: Skips First Line when reading data from text file

    Have you even read your code? You certainly appear not to have debugged it.
    Code:
            Dim i As Long = 0
            Dim sr As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Dim line As String = sr.ReadLine()
            Dim ItemIDNumber As String = ""
            Dim AddNewRow As Boolean = True
            Dim Msg1 As Object, Msg2 As Object
            Dim Qty As Object
            Dim Total As Object
    
    
    
            Do Until (sr.EndOfStream)
    
                line = sr.ReadLine()
                Dim fields() As String = line.Split(",")
                '
                MessageBox.Show(fields(0))
                MessageBox.Show(fields(1))
                '
          Loop
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    228

    Re: Skips First Line when reading data from text file

    Thanks jmcilhinney for your reply

    Still it skips first line, I deleted all data from text file, Just kept first line only then it does not enter in loop also.


    Ladak

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,417

    Re: Skips First Line when reading data from text file

    jmcilhinney was only indicating the cause of problem (in the hope you would think about what your code is doing, and work out how to fix it), he was not giving the solution.

    The variable declaration should be changed, so that it doesn't read a line from the file (which then gets ignored), eg:
    Code:
    Dim line As String = ""

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    Re: Skips First Line when reading data from text file

    Quote Originally Posted by accmaster View Post
    Just kept first line only then it does not enter in loop also.
    Congratulations on doing the exact opposite of what you should have. Read the code! You read a line, then you enter the loop and you repeatedly read a line and and display its contents. What exactly are you expecting to happen to that first line that you read before entering the loop?

    This is a PERFECT example of what happens when you right code without a clear idea of what it is supposed to do. If you had picked up a pen and paper and written down the steps you needed to perform to achieve your aim then you'd have something to actually compare your code to and then it would be obvious that what you have makes no sense.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    228

    Re: Skips First Line when reading data from text file

    Thanks si_the_geek for your reply,

    The issue is if i have only one row in next file, on executing the code it does not enter in loop.
    This shows it is not reading file line.

    After changing Dim line As String = "" Issue is still there.

    If there is any solution please let me know

    Thanks

  7. #7
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    603

    Re: Skips First Line when reading data from text file

    Quote Originally Posted by accmaster View Post
    Thanks si_the_geek for your reply,

    The issue is if i have only one row in next file, on executing the code it does not enter in loop.
    This shows it is not reading file line.

    After changing Dim line As String = "" Issue is still there.

    If there is any solution please let me know

    Thanks
    Have you tried stepping through your code in a debugger? If not, why not? If you have then at what point does it do something different to what you expect it to do? Do the various variable seem to have sensible values while debugging?

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,143

    Re: Skips First Line when reading data from text file

    JMC, he's not reading your posts because you're being condescending and ugly. Smart people ignore people like that, even though sometimes they happen to have the right answer. A person can "not understand what they're doing" without "being an idiot", much like we all understand you should've known how to spell your words properly.

    OP, as impossible as it might seem he had something important to say. Imagine this is what's in your text file.
    Code:
    0
    1
    2
    3
    Every time you call ReadLine(), the reader takes one line out and moves to the next. Every. Time. There's no way to call ReadLine() without doing that. So let's look at your code:

    Code:
    Dim i As Long = 0
    Dim sr As StreamReader = New StreamReader(OpenFileDialog1.FileName)
    Dim line As String = sr.ReadLine()
    That's the first ReadLine() in your program. It puts "0" in the variable 'line', and moves to the next line in the file. Let's see if you use that value.
    Code:
    Dim ItemIDNumber As String = ""
    Dim AddNewRow As Boolean = True
    Dim Msg1 As Object, Msg2 As Object
    Dim Qty As Object
    Dim Total As Object
    
    Do Until (sr.EndOfStream)
        line = sr.ReadLine()
    Hmm. There's the next ReadLine(), and you haven't used 'line' yet. That means it will put "1" into the variable, then move to the next line.

    So oops! The line is being skipped because you didn't think about the consequences of calling ReadLine. You wanted to initialize 'line' to something, but if you call ReadLine() you have to make sure you use the value you read. I suggest you change your code to this:
    Code:
    Dim line As String = ""
    That way you aren't reading "too early", but you still have the variable initialized to something.
    Last edited by Sitten Spynne; Oct 4th, 2017 at 08:52 AM.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,049

    Re: Skips First Line when reading data from text file

    Quote Originally Posted by Sitten Spynne View Post
    much like we all understand you should've known how to spell your words properly.
    Huh? You've lost me there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,096

    Re: Skips First Line when reading data from text file

    This thread has gotten off topic, and been pruned...by somebody....I think. Let's get off the personal comments and back to the topic. I understand the frustrations on all sides, but it's not getting anybody anywhere.
    My usual boring signature: Nothing

  11. #11
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,001

    Re: Skips First Line when reading data from text file

    Quote Originally Posted by jmcilhinney View Post
    Huh? You've lost me there.
    I think he meant you wrote "right" instead of "write"
    In order to understand recursion, one must first understand recursion….

  12. #12
    Fanatic Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    718

    Re: Skips First Line when reading data from text file

    Hi accmaster,

    this is a simple example..

    the Textfile has a delimeter ;
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            With DataGridView1
                .Columns.Add("1", "1")
                .Columns.Add("2", "2")
                .Columns.Add("3", "3")
                .Columns.Add("4", "4")
                .Columns.Add("5", "5")
            End With
    
    
            For Each line As String In System.IO.File.ReadAllLines("C:\testData.txt")
                DataGridView1.Rows.Add(line.Split(";"))
            Next
    
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.