Next button help

Steven Low

Active member
Joined
Apr 14, 2005
Messages
42
Programming Experience
1-3
Hi Guys

Im am trying to get the next button and previouse button to work properly with validations. Ive created a method SetButtonState which handles the button state. I am having a problem with the next button. Cant get it to work properly. I want it to start from the start and end at the last row. Theres a total of 5 rows...

Need help:confused:

VB.NET:
Code: 
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click 

        'Show the next record if the current one is not the last 
        Dim connStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Project\proj.mdb;") 
        Dim sqlStr As String = "SELECT * FROM Student" 
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) 

        'Show the next record if the current one is not the last 
  

        dataAdapter.Fill(dt) 
        dataAdapter.Dispose() 
        UpdateTextBoxes() 


        If (rowIndex < dt.Rows.Count - 1) Then 
            rowIndex += 1 'Increase the rowIndex by 1 
            'MessageBox.Show("thats it") 
            UpdateTextBoxes() 
            shows() 

        End If 

        SetButtonState(rowIndex, dt.Rows.Count) 

    End Sub 

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click 
        'Show the previous record if the current one is not the first 


        Dim connStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C[IMG]http://www.vbcity.com/forums/smiles/tongue.gif[/IMG]roj.mdb;") 
        Dim sqlStr As String = "SELECT * FROM Student" 
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) 

        dataAdapter.Fill(dt) 
        dataAdapter.Dispose() 
        UpdateTextBoxes() 

        If (rowIndex > 0) Then 
            rowIndex = rowIndex - 1 

            UpdateTextBoxes() 

        End If 

        SetButtonState(rowIndex, dt.Rows.Count) 

    End Sub 
 Private Sub SetButtonState(ByVal CurrRowIndex As String, ByVal NumberOfRows As String) 

        'Set the Previous Button State 
        Select Case True 
            Case CurrRowIndex = 0 
                'Indicates its on the first row 
                btnPrevious.Enabled = False 
            Case NumberOfRows = 1 
                'There is only 1 row in the DataTable 
                btnPrevious.Enabled = False 
            Case Else 
                btnPrevious.Enabled = True 
        End Select 

        'Set the Next Button State 
        Select Case True 
            Case NumberOfRows = 1 
                'There is only 1 row in the DataTable 
                btnNext.Enabled = False 
            Case CurrRowIndex = NumberOfRows - 1 
                'Currently on the last Row 
                btnNext.Enabled = False 
            Case Else 
                btnNext.Enabled = True 
        End Select 

    End Sub
 
try this,

VB.NET:
OnFormLoad()

        Dim connStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Project\proj.mdb;") 
        Dim sqlStr As String = "SELECT * FROM Student" 
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) 
  
        dataAdapter.Fill(dt) 

        currentRow=0
        totalRow=dt.rows.count()

        'since it still in the first record then disable the prev button
        PrevButton.enabled=false;

        'display the data
        NameLabel.text=cstr(dt.rows(currentRow)("Name")) 

NextButton onClick()

        if currentRow<totalRow-1 then        
           currentRow=currentRow+1
        end if

        if currentRow=totalRow-1 then
           NextButton.enabled=false
        end if

        PrevButton.enabled=true

        'display the data in the dataTable here
        NameLabel.text=cstr(dt.rows(currentRow)("Name"))

PrevButton onClick()

        if currentRow>0 then
           currentRow=currentRow-1
        end if

        if currentRow=0 then
           PrevButton.enabled=false
        end if

        NextButton.enabled=true

        'display the data in the dataTable here
        NameLabel.text=cstr(dt.rows(currentRow)("Name"))
 
Hi Siddu
Heres my update method within the button

the reason for commandbuilder.QuotePrefix = "[" is because

is because some of my colums contain spaces like "first name"
this fixes it try it and let me know


VB.NET:
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim changes AsInteger
 
Dim commandbuilder AsNew OleDb.OleDbCommandBuilder(oledbstudent)
Me.BindingContext(oDataTable).EndCurrentEdit()
commandbuilder.QuotePrefix = "["
commandbuilder.QuoteSuffix = "]"
changes = oledbstudent.Update(oDataTable)
 
 
connection.Open()
' dg1.ExecuteNonQuery()
connection.Close()
data()
EndSub
 
Last edited by a moderator:
Back
Top