Hi all, just having problems with my next, previous, last and first buttons on my form. there not working but i am not sure where i am going wrong because i am a beginner. When i press any of the buttons it says 'there is no row at position 1' when there is on my datagridview and on my database as well.

Public Class Stock_Control

    Dim dbConnection As OleDbConnection
    Dim dbcommand As OleDbCommand
    Dim dbdataAdapter As OleDbDataAdapter
    Dim connectstring As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "data source = COMP4.mdb"
    Dim dtStockControl As DataTable

    Dim inc As Integer
    Dim MaxRows As Integer


    Private Sub Stock_Control_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbConnection = New OleDbConnection(connectstring)
        dbdataAdapter = New OleDbDataAdapter()
        dtStockControl = New DataTable()
        dbcommand = New OleDbCommand("Stock Control")
        dbcommand.CommandType = CommandType.TableDirect

        dbdataAdapter.SelectCommand = dbcommand
        dbdataAdapter.SelectCommand.Connection = dbConnection
        dbConnection.Open()
        dbdataAdapter.Fill(dtStockControl)
        grdStockControl.DataSource = dtStockControl
        dbConnection.Close()

        MaxRows = COMP4DataSet.Tables("Stock Control").Rows.Count
        inc = -1

    End Sub

    Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click
        If inc <> MaxRows - 1 Then
        inc = inc + 1
        NavigateRecords()
         Else
        MsgBox("No More Rows")
         End If

    Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
          If inc > 0 Then
        inc = inc - 1
         NavigateRecords()
         Else
         MsgBox("First Record")
         End If

    End Sub

    Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
         If inc <> 0 Then
        inc = 0
           NavigateRecords()
         End If

    End Sub

    Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
        If inc <> MaxRows - 1 Then
        inc = MaxRows - 1
        NavigateRecords()
         End If

    Private Sub NavigateRecords()

        'txtProductType.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(0)
        'txtMake.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(1)
        'txtPartNo.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(2)
        'txtModel.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(3)
        'txtWholesalerPrice.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(4)
        'txtRetailPrice.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(4)
        'txtSupplierUsed.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(6)
        'txtQuantity.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(7)
        'txtReOrderLevel.Text = COMP4DataSet.Tables("Stock Control").Rows(inc).Item(8)

    End Sub

End Class

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Use DataBindings technique.

Take a look,

Public Class Stock_Control

    Dim dbConnection As OleDbConnection
    Dim dbcommand As OleDbCommand
    Dim dbdataAdapter As OleDbDataAdapter
    Dim connectstring As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "data source = COMP4.mdb"
    Dim dtStockControl As DataTable

    Dim inc As Integer
    Dim MaxRows As Integer


    Private Sub Stock_Control_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbConnection = New OleDbConnection(connectstring)
        dbdataAdapter = New OleDbDataAdapter()
        dtStockControl = New DataTable()
        dbcommand = New OleDbCommand("Stock Control")
        dbcommand.CommandType = CommandType.TableDirect

        dbdataAdapter.SelectCommand = dbcommand
        dbdataAdapter.SelectCommand.Connection = dbConnection
       
        dbdataAdapter.Fill(dtStockControl)
        grdStockControl.DataSource = dtStockControl
        
        'DataBindings

         txtProductType.DataBindings.Add("Text",dtStockControl,"Producttype")
txtMake.DataBindings.Add("Text",dtStockControl,"Make")
    
    End Sub

    Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click
       Me.BindingContenxt(dtStockControl).Position+=1
End sum  
    Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
               Me.BindingContenxt(dtStockControl).Position-=1
    End Sub

    Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
               Me.BindingContenxt(dtStockControl).Position=0
    End Sub

    Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
             Me.BindingContenxt(dtStockControl).Position=Me.BindingContenxt(dtStockControl).Count
End Sub
End Class

Use DataBindings technique.

Take a look,

Public Class Stock_Control

    Dim dbConnection As OleDbConnection
    Dim dbcommand As OleDbCommand
    Dim dbdataAdapter As OleDbDataAdapter
    Dim connectstring As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "data source = COMP4.mdb"
    Dim dtStockControl As DataTable

    Dim inc As Integer
    Dim MaxRows As Integer


    Private Sub Stock_Control_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbConnection = New OleDbConnection(connectstring)
        dbdataAdapter = New OleDbDataAdapter()
        dtStockControl = New DataTable()
        dbcommand = New OleDbCommand("Stock Control")
        dbcommand.CommandType = CommandType.TableDirect

        dbdataAdapter.SelectCommand = dbcommand
        dbdataAdapter.SelectCommand.Connection = dbConnection
       
        dbdataAdapter.Fill(dtStockControl)
        grdStockControl.DataSource = dtStockControl
        
        'DataBindings

         txtProductType.DataBindings.Add("Text",dtStockControl,"Producttype")
txtMake.DataBindings.Add("Text",dtStockControl,"Make")
    
    End Sub

    Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click
       Me.BindingContenxt(dtStockControl).Position+=1
End sum  
    Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
               Me.BindingContenxt(dtStockControl).Position-=1
    End Sub

    Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
               Me.BindingContenxt(dtStockControl).Position=0
    End Sub

    Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
             Me.BindingContenxt(dtStockControl).Position=Me.BindingContenxt(dtStockControl).Count
End Sub
End Class

I was wondering would you be able to give me advice or help me with being able to update a row in my datagrid because i have looked at homeandlearn and there binding textboxes method is obviously different to mine. i have tried doing trial and error and it hasnt really got me far. I could show you my code i have come up with if it helps. Any help would be great thanks

This question has already been answered. Start a new discussion instead.