vb.netmovebindingsourcebindingnavigator

vb.net bindingSource and bindingNavigation programmatically


Please have a look to the following code and tell me why it does not move to the next record? I load data programmatically and fill tables in dataset. I could do this by wizard but I want to do it with my own code; because using wizard sometimes does not help understanding the real code behind it.

Private Sub frmSystemOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
                dsOptions = New DataSet
                loadOptions()
                bsInstitute = New BindingSource(dsOptions, "institute") 
                bnInstitute = New BindingNavigator(bsInstitute)

                InstIdTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"),"instId")
                CodeTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "code")
                NameTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "name")
                TypeTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "type")

            Catch ex As Exception
                MsgBox(Err.Description)
            End Try

        End Sub

        Sub loadOptions()
            Dim sql As String

            Try
                sqlConn = New SqlConnection(connString)
                sqlConn.Open()

                sql = "select * from institute"
                daAdapter = New SqlDataAdapter(sql, sqlConn)
                daAdapter.Fill(dsOptions, "institute")
                '----------------------------------------------------------------------

                sqlConn.Close()
            Catch ex As Exception
                sqlConn.Close()
                MsgBox(Err.Description)
            End Try
        End Sub

        Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
            If bsInstitute.Position + 1 < bsInstitute.Count Then
                bsInstitute.MoveNext()
            Else
                bsInstitute.MoveFirst()
            End If

            Me.Validate()

        End Sub

Solution

  • I found the solution. The databounds should be as the following:

    InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId") CodeTextBox.DataBindings.Add("Text", bsInstitute, "code") NameTextBox.DataBindings.Add("Text", bsInstitute, "name") TypeTextBox.DataBindings.Add("Text", bsInstitute, "type") 
    dataset instead of bsInstitute. But now its perfect.    Try
                dsOptions = New DataSet
                loadOptions()
    
                bsInstitute = New BindingSource(dsOptions, "institute")
    
                InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")
                CodeTextBox.DataBindings.Add("Text", bsInstitute, "code")
                NameTextBox.DataBindings.Add("Text", bsInstitute, "name")
                TypeTextBox.DataBindings.Add("Text", bsInstitute, "type")
    
            Catch ex As Exception
                MsgBox(Err.Description)
            End Try
    

    I was using the dataset for the binding like this

    InstIdTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"),"instId")
    

    But whats right I should replace the dsOptions.tables("institute") in the above line of code with the bindingSource I creadted like the following

    InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")
    

    This way I could use the bindingSource object to navigate records in my dataset.