Click here to Skip to main content
16,020,447 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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?
VB
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
       
        NavigateRecords()
    End Sub

    Private Sub NavigateRecords()

        Dim con As New OleDb.OleDbConnection
        Dim strCon As String = "Provider=SQLOLEDB; Data Source=192.168.131.72; Initial Catalog=Flex; User ID=sa; Password="

        con.ConnectionString = strCon

        con.Open()
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter

        Sql = "SELECT * FROM Customer"

        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "Person")

        con.Close()
        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

            NavigateRecords()

        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

            NavigateRecords()

        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

            NavigateRecords()
        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

            NavigateRecords()

        End If
    End Sub

End Class
Posted
Updated 15-Nov-12 3:03am
v2

1 solution

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.
 
Share this answer
 
Comments
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?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900