next record vb 2005

KLin816

Member
Joined
Mar 5, 2008
Messages
10
Programming Experience
Beginner
hi,

i have a botton called next for going through filtered records. so if the search yield 10 records i can only see the first 9 record and it will give me an error after i click the next botton to see the 10th record. anyone has any idea? the error message is "There is no row at position 10" but there are 10 records.:confused:

thank you.

VB.NET:
    Private Sub btmNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmNext.Click

        If dt4.Rows.Count = True Then
            btmNext.Enabled = False
            btmPrev.Enabled = True
        Else
            btmPrev.Enabled = True
            btmNext.Enabled = True
            int_count = int_count + 1
            If int_count = dt4.Rows.Count Then
                btmNext.Enabled = False
            End If

            lbldbcur.Text = int_count
            lbldbCount.Text = dt4.Rows.Count
            txtdbDivision.Text = ds4.Tables(0).Rows(int_count).Item(4)
            txtdbPortNo.Text = ds4.Tables(0).Rows(int_count).Item(5)
            txtdbWallStreet.Text = ds4.Tables(0).Rows(int_count).Item(12)
            txtdbExp.Text = ds4.Tables(0).Rows(int_count).Item(2)
            txtdbFamName.Text = ds4.Tables(0).Rows(int_count).Item(17)
            txtdbPortName.Text = ds4.Tables(0).Rows(int_count).Item(3)
            txtdbDom.Text = ds4.Tables(0).Rows(int_count).Item(27)
            txtdbGuideLim.Text = Format(CSng(ds4.Tables(0).Rows(int_count).Item(7)), "$###,###,###,###,###,###")
            txtdbCurRisk.Text = Format(CSng(ds4.Tables(0).Rows(int_count).Item(9)), "$###,###,###,###,###,###")
            txtdbSetLim.Text = Format(CSng(ds4.Tables(0).Rows(int_count).Item(8)), "$###,###,###,###,###,###")
            txtdbFundType.Text = ds4.Tables(0).Rows(int_count).Item(21)
            txtdbInvMgr.Text = ds4.Tables(0).Rows(int_count).Item(11)
            txtdbComments.Text = ds4.Tables(0).Rows(int_count).Item(23)
            txtdbConf_Port.Text = ds4.Tables(0).Rows(int_count).Item(28)
            txtdbLoc.Text = ds4.Tables(0).Rows(int_count).Item(6)
        End If
    End Sub
 
Since I don't see where you're dimensioning int_count I'm going to assume you're starting at index 0.

The problem would be with this line
VB.NET:
int_count = int_count + 1
. You're increasing the count before you're trying to show the row.

The problem with is is that when you're trying to show your 10th row (index 9) you're really calling the 11th record (index 10).

Try incrementing your int_count variable after the fact.
 
hmm... i do see your point. but i actually set int_count = 1 in my ealier code because when i search the database if i get 10 record from the search the first record is already shown, so it would be 1 of 10 then if i hit the next botton with this int_count = int_count + 1, then i would get 2 of 10 right. am i still making any sense? :p
 
I don't understand this line:
VB.NET:
If dt4.Rows.Count = True Then
Rows.Count is an integer (total number of rows) and 'True' in Integer form is 1, 'False' is 0
 
I under stand what you're saying.

You're actually showing index 1 before you hit btnNext the first time which is really the 2nd record.

Your table is 0 index based so while you would think to count 1, 2, 3, 4, 5... your table is starting at 0, 1, 2, 3, 4...

Try initializing int_count to 0 and going to Rows.Count - 1
 
I think we're having a language disconnect here.

You know there are 10 records that you need to cycle through because of:

VB.NET:
dt4.rows.count

The first row in your dataset is row 0.

VB.NET:
ds4.Tables.Rows([B]0[/B])

The last row in your dataset is row 9

VB.NET:
ds4.Tables.Rows([B]9[/B])

since you've set int_count to = 1 you're showing the second row of your data set initially in error.

VB.NET:
ds4.Tables.Rows([B]1[/B])

It's erroring out when you try

VB.NET:
ds4.Tables.Rows([B]10[/B])

-----
To fix your problem

VB.NET:
Dim int_count As Integer = 0

Since your int_count on the 10th record is going to be 9 you need to account for this when disabling btnNext

VB.NET:
If int_count = dt4.Rows.Count - 1 Then
     btmNext.Enabled = False
End If

You'll also want to change the label depicting your current position to reflect a count starting a 1.

VB.NET:
lbldbcur.Text = int_count + 1
 
hi,

i have a botton called next for going through filtered records. so if the search yield 10 records i can only see the first 9 record and it will give me an error after i click the next botton to see the 10th record. anyone has any idea? the error message is "There is no row at position 10" but there are 10 records.:confused:

There are 10 records but the filter is only showing 9 of them. This is entirely unrelated to the concept that a collection of 10 elements runs from positions 0 to 9. there is no row at position 10 because a table of 10 rows does not run from 1 to 10, but 0 to 9..
 

Latest posts

Back
Top