Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
HI FRIENDS I TO WANT DISPLAY DATABASE DATA IN TO TEXT BOXES HERE IAM USING FIRST NEXT PREVIOUS LAST BUTTONS..

I HAVE WRITTEN THE CODE BUT ITS NOT WORKING PROPERLY IT WORKS FOR FIRST AND LAST BUTTONS CLICK EVENTS IT DISPLAYS DATABASE DATA INTO TEXT BOXES BUT WHEN IAM CLICK NEXT AND PREVIOUS BUTTONS IT DOES NOT WORK PROPERLY IT WORKS ONLY DISPLAYS FIRST TIME CORRECTLY SECOND TIME IAM CLICKING PREVIOUS BUTTON IT DOES NOT DISPLAY PREVIOUS RECORD IN THE TEXTBOXES IT DISPLAYS SAME RECORD IN TEXTBOX.

HERE IS MY CODE PLEASE HELP ME FOR THIS PROBLEM..

THANKING YOU FRIENDS....

VB
Dim rno As Integer
        cmd.CommandText = " select * from customerr"
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        da.SelectCommand = cmd
        cmd.Connection = connection
        da.Fill(dt)

        rno = dt.Rows.Count - 1
        If rno > 0 Then
            rno -= 1
            If dt.Rows.Count > "0" Then
                txtcustid.Text = dt.Rows(rno)(0).ToString()
                txtcustname.Text = dt.Rows(rno)(1).ToString()
                txtcustaddress.Text = dt.Rows(rno)(2).ToString()
                
            End If
        End If



        cmd.CommandText = "select * from customerr"
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        da.SelectCommand = cmd
        cmd.Connection = connection
        da.Fill(dt)
       
        rno = dt.Rows.Count - 1
        If rno < dt.Rows.Count - 1 Then
            rno += 1
            If dt.Rows.Count > 0 Then
                txtcustid.Text = dt.Rows(rno)(0).ToString()
                txtcustname.Text = dt.Rows(rno)(1).ToString()
                txtcustaddress.Text = dt.Rows(rno)(2).ToString()
               
            End If
        End If
Posted
Updated 4-Sep-14 22:56pm
v3
Comments
Sinisa Hajnal 5-Sep-14 2:08am    
Commonly, capital letters on the web means you're shouting. Please quiet down. ;)

Your solution would work for first and last customer only because you're always looking at
rno = dt.Rows.Count -1 (i.e. you're never going forward or backward)

What you need is put a variable into member variable (say _currentRowIndex) which you would then increase / decrease.

You would load the table ONCE and set _currentRow to zero and show dt.Rows(0). Then on Next click you would
VB
' This will prevent your next from going over last item (alternatively you can set it to zero and not return thus starting from the first)
        If _currentRow +1 > dt.Rows.Count - 1 Then return

        _currentRow += 1
        With dt.Rows(0)
             txtcustid.Text = .Item("customer_id").ToString()
             txtcustname.Text = .Item("customer_name").ToString()
             txtcustaddress.Text = .Item("customer_addresss).ToString()
        End With


With Prev button you do the same, except you're comparing with zero instead of dt.rows.count-1

NOTE: I used column names instead of indexes filling fields. This has several advantages:
- you don't have to remember which index is which (in your case textbox names give a clue, but what happens when you add new field or change the order of the existing ones?)
- when you get back to it in several months, you will not have to recheck your field order - especially if there are more then those three.
 
Share this answer
 
v2
FRIEND here _currentRow is "0" or "1"
 
Share this answer
 

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