Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear friends
Sarfaraz here. I am now moving to VB.net and gradually trying to learn the language thanks to you all for support on my earlier questions in VB6.

Friends I have connected My sql server table with vb.net and could display all the records in a datagrid. But I have a problem in moving the records and bind them to text boxes. The problem is that i could only bind the last record.

My code is is working to load last record in the text boxes but how to move to Next-Previous-First-Last Record by clicking on command buttons for Next-Previous-First-Last Record and display them in text boxes accordingly.
VB
sql = "SELECT * FROM users"
          con.Open()
          cmd.CommandText = sql
          cmd.Connection = con
          dataadapter.SelectCommand = cmd
          datareader = cmd.ExecuteReader
          While datareader.Read
              datareader.Read()
              TextBox1.Text = datareader("UserId")
              TextBox2.Text = datareader("Username")
              TextBox3.Text = datareader("Age")
          End While

          con.Close()

Please help
Thank you
Posted
Updated 11-Jan-13 5:57am
v3

You haven't bound anything.

All you did was put the contents of the records, one at time into the textbox. With each record you iteracted over, you replaced the contents of the textboxes with the contents of the latest record.

Try reading up on BindingNavigator[^] and maybe watch this[^].
 
Share this answer
 
Dear all
I try and finally come up with the solutions.
The code is as under:-

VB
Imports MySql.Data.MySqlClient

Public Class Form1
    Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=students;user id=root;password=root")
    Dim ds As DataSet = New DataSet
    Dim dataadapter As New MySqlDataAdapter("select * from users", con)
    Dim cmd As MySqlCommand = New MySqlCommand()
    Dim dv As DataView
    Dim cm As CurrencyManager
    Dim datareader As MySqlDataReader

    Public Sub filldatasetandview()
        ds = New DataSet
        dataadapter.Fill(ds, "users")
        dv = New DataView(ds.Tables("users"))
        cm = CType(Me.BindingContext(dv), CurrencyManager)
    End Sub
    Public Sub bindfields()
        TextBox1.DataBindings.Clear()
        TextBox2.DataBindings.Clear()
        TextBox3.DataBindings.Clear()
        TextBox1.DataBindings.Add("text", dv, "userid")
        TextBox2.DataBindings.Add("text", dv, "username")
        TextBox3.DataBindings.Add("text", dv, "age")
    End Sub
    Public Sub showposition()
        TextBox4.Text = cm.Position + 1 & "     " & "of" & "    " & cm.Count()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        filldatasetandview()
        bindfields()
        showposition()
    End Sub

    Private Sub previousrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previousrecord.Click
        cm.Position = cm.Position - 1
        showposition()
    End Sub

    Private Sub nextrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextrecord.Click
        cm.Position = cm.Position + 1
        showposition()
    End Sub

    Private Sub lastrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastrecord.Click
        cm.Position = cm.Count - 1
        showposition()
    End Sub

    Private Sub firstrecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstrecord.Click
        cm.Position = 0
        showposition()
    End Sub
End Class
 
Share this answer
 
Comments
Member 13550326 15-Dec-17 20:22pm    
Above code worked great for moving data, but when I need to find a record I have to run a query and then display the result on the form in there particular text boxes. I have created a new dataset, dataview and currencymanager to display the result but then when I click next,first,last,previous buttons nothing displays as there are no records in new dataset.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900