注册 登录
编程论坛 VB.NET论坛

幫我看下問題(新問題)

jxyga111 发布于 2011-07-11 13:39, 515 次点击
private sub command1_click()
  Dim strcn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\t_login.mdb"
            Dim cn As New OleDbConnection(strcn)
            '  Dim command As OleDbCommand
            Dim ap As New OleDbDataAdapter
            Dim ds As New DataSet
            cn.Open()
            Dim mysql As String
            mysql = "select id,password1 from t_login"
            ap = New OleDbDataAdapter(mysql, cn)
            ap.Fill(ds, "t_login")
            cn.Close()
            Me.TextBox1.DataBindings.Add("text", ds, "t_login.id")
            Me.TextBox2.DataBindings.Add("text", ds, "t_login.password1")

        Catch ex As Exception
            MsgBox("有錯我就出現")
            Exit Sub
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.TextBox1.BindingContext(ds, "t_login").Position = 0
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.TextBox1.BindingContext(ds, "t_login").Position += 1
    End Sub
End Class
點觸發了,下一 頁後,再點顯示,不會重新檢索數據,還是下一頁那筆數據

[ 本帖最后由 jxyga111 于 2011-7-12 11:59 编辑 ]
4 回复
#2
不说也罢2011-07-11 18:03
1、如果你写了MsgBox(ex.Message),就知道错误出现在哪里了。
2、这是因为你在button3中BindingContext所使用的DATASET与实际绑定的DATASET不一致所导致的。

我给你改了一下:
程序代码:
    Dim ds As New DataSet
    Private Sub command1_click() Handles Button1.Click
        Try
            Me.TextBox1.DataBindings.Clear()
            Me.TextBox2.DataBindings.Clear()
            Dim strcn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\t_login.mdb"
            Dim cn As New OleDbConnection(strcn)
            '  Dim command As OleDbCommand
            Dim ap As New OleDbDataAdapter
            cn.Open()
            Dim mysql As String
            mysql = "select id,password1 from t_login"
            ap = New OleDbDataAdapter(mysql, cn)
            ap.Fill(ds, "t_login")
            Me.TextBox1.DataBindings.Add("text", ds, "t_login.id")
            Me.TextBox2.DataBindings.Add("text", ds, "t_login.password1")
            cn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox("有錯我就出現")
            Exit Sub
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.TextBox1.BindingContext(ds, "t_login").Position = 0
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.TextBox1.BindingContext(ds, "t_login").Position += 1

    End Sub
#3
jxyga1112011-07-12 11:09
那個我知道問題所在了。因為文本的內容差生錯誤
那為何我做的下一條記錄不行。此方案是按教程來的
#4
jxyga1112011-07-12 11:19
已解決謝謝
#5
不说也罢2011-07-12 12:07
你说的新问题其实还是老问题吧。再次绑定前要清除原有的绑定
Me.TextBox1.DataBindings.Clear()
Me.TextBox2.DataBindings.Clear()
这两句你没有写哦
再加上一句ds.Clear()'重新初始化DATASET

[ 本帖最后由 不说也罢 于 2011-7-12 12:12 编辑 ]
1