Results 1 to 2 of 2

Thread: No row at position 0

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    No row at position 0

    Hi guys, i'm kinda stuck again and i know that you geniuses will probably be able to help me out

    ok so i've been creating a program in VB that connects to a Microsoft database, and up until now, the program has been displaying the information fine. however, all i get now is the error message "No row at position 0" whenever it tries to display almost anything.

    I am baffled as i have 3 forms that work and are programmed exactly the same way and only 1 works fine :/

    HEELLLPP!

    Code:
            MaxRows = ds.Tables("TTDB").Rows.Count
    
            Do
    
                ListBox1.Items(inc) = ds.Tables("TTDB").Rows(inc).Item("TeacherName") - error message from this segment
    
                inc = inc + 1
    
            Loop Until inc = MaxRows

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: No row at position 0

    The error message speaks for itself. There is no row at position 0 so you cannot get the row at position 0. Your Do loop should be using a pre-condition to allow for that rather than a post-condition:
    vb Code:
    1. MaxRows = ds.Tables("TTDB").Rows.Count
    2.  
    3.         Do Until inc = MaxRows
    4.  
    5.             ListBox1.Items(inc) = ds.Tables("TTDB").Rows(inc).Item("TeacherName") - error message from this segment
    6.  
    7.             inc = inc + 1
    8.  
    9.         Loop
    Now the condition will be tested BEFORE each iteration so you'll never enter the loop if there aren't any rows.

    That said, you really shouldn't be using a Do loop there anyway. a Do loop should be used for a condition not related to counting. If you can count the iterations then a For loop is far more suitable than a Do loop:
    vb.net Code:
    1. Dim rows As DataRowCollection = ds.Tables("TTDB").Rows
    2.  
    3. For i As Integer = 0 To rows.Count - 1
    4.     ListBox1.Items(i) =rows(i)("TeacherName")
    5. Next
    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

Posting Permissions

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



Click Here to Expand Forum to Full Width