none
vb.net读取下一条数据库的数据问题 RRS feed

  • 问题

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

            recordfind()

            BindingNavigatorMoveNextItem.Enabled = True

    End Sub

    Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click

            Dim recno As Integer

     

            Try

                recno += 1

               Dim ds As New DataSet()

                TextBox1.Text = ds.Tables("Sales").Rows(recno).Item("SO_QTTY")

            Catch ex As Exception

                MsgBox("End of data!")

            End Try

        End Sub

    Private Sub recordfind()

            Dim objCon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\INVILL.accdb;")

            Dim objComm As New OleDbCommand("select * from Sales", objCon)

            Dim da As New OleDbDataAdapter(objComm)

            Dim ds As New DataSet()

            da.Fill(ds, "Sales")

            TextBox1.DataBindings.Add("text", ds.Tables("Sales").DefaultView, "SO_QTTY")

     

        End Sub

    Private Sub details()

                TextBox1.Text = ds.Tables("Sales").Rows(recno).Item("SO_QTTY")

     

        End Sub

    1.数据库有10个数据,可是为什么当我按下一条时,却提示“End of data”,是哪里写错了?

    2.  为什么toolstripbutton在运行时,不能操作,就好像disable了(显示灰色)。我要在form load  BindingNavigatorMoveNextItem.Enabled = True才可以运行。我在toolstripbuttonenable打勾,也是不能运行。

    2010年10月1日 15:58

答案

  • 你好
    這可能是因為你沒有初始化recno 所以NOTHING + 1 會出現問題的

    或者你可以嘗試預先定義你的recno為0

    E.G. 可以嘗試改變你的CODE 像如下

            Private recno As Integer

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

            recordfind()

    recno = 0

            BindingNavigatorMoveNextItem.Enabled = True

    End Sub

    Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click

    '        Dim recno As Integer '把他放到 METHOD 外

     

            Try

                recno += 1

                Dim ds As New DataSet()

                TextBox1.Text = ds.Tables("Sales" ).Rows(recno).Item("SO_QTTY" )

            Catch ex As Exception

                MsgBox("End of data!" )

            End Try

        End Sub

    Please correct me if my concept is wrong


    Chi
    2010年10月2日 9:00
    版主