Click here to Skip to main content
12,251,027 members (39,133 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I have 3 text boxes that retrieve data from the database and 4 buttons fist, previous, next and last that help me to navigate through the records of the table.
But I have a problem with the code of the button previews and next.
Because when I execute the application, the next button takes only one value (row=2) and always when I press the next button it leads me to the second record of the table.
the same thing happens with the previous button, it takes only the first record of the table.
Please tell me what I have made wrong to my code?
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class default
    Inherits System.Web.UI.Page
    Dim Sql As String
    Dim inc As Integer = 0
    Dim ds As New DataSet
    Dim MaxRows As Integer
    Dim SQLConnection As Object
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Sub NavigateRecords()
        Dim con As New OleDb.OleDbConnection
        Dim strCon As String = "Provider=SQLOLEDB; Data Source=; Initial Catalog=Flex; User ID=sa; Password="
        con.ConnectionString = strCon
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Sql = "SELECT * FROM Customer"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "Person")
        MaxRows = ds.Tables("Person").Rows.Count
        txt_name.Value = ds.Tables("Person").Rows(inc).Item("2")
        txt_surname.Value = ds.Tables("Person").Rows(inc).Item("3")
        txt_city.Value = ds.Tables("Person").Rows(inc).Item("4")
    End Sub
    Protected Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
        If (inc <> (MaxRows - 1)) Then
            inc = inc + 1
        End If
    End Sub
    Protected Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
        If (inc <> 0) Then
            inc = 0
        End If
    End Sub
    Protected Sub btn_previews_Click(sender As Object, e As EventArgs) Handles btn_previews.Click
        If (inc > 0) Then
            inc = inc - 1
        End If
    End Sub
    Protected Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
        If (inc <> (MaxRows - 1)) Then
            inc = MaxRows - 1
        End If
    End Sub
End Class
Posted 15-Nov-12 2:58am
Edited 15-Nov-12 3:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is pretty bad code. You read ALL the values when you only want to show one. ROW_NUMBER[^] is the SQL Server way to select a row.
lovitaxxx 15-Nov-12 9:15am
Where should i use ROW_NUMBER?
Christian Graus 15-Nov-12 9:18am
It's SQL, so in your SQL statement. You should use it to request only the row number you want to show.
lovitaxxx 15-Nov-12 9:54am
are u telling me to use this code:
Sql = "SELECT name,surname,city,ROW_NUMBER() FROM Customer"
sorry for my'stupid' questions but i am new in this
Christian Graus 15-Nov-12 9:58am
Did you read the link ? No, that syntax does not work at all, nor does it help. You need to do a CONDITIONAL select, based on row number.
lovitaxxx 15-Nov-12 10:11am
can you illuminate me more pls?
Christian Graus 15-Nov-12 10:20am
So you have the documentation for a way to get row numbers. You want to both get the row number and use it as a filter, so you select only one row instead of the whole DB. In general, if you're 'new at this', you should consider that if you're being paid, you're stealing from someone, and if you're not, then you should select a more basic task to ease your learning curve so you learn to program, rather than learn how to ask us to do it for you.
lovitaxxx 20-Nov-12 9:25am
I was learning from this book for more than a week
Here i learned how to navigate through records.
But it seems that i have waisted my time for nothing :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100