Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
  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 11-Jan-13 4:37am
Edited 11-Jan-13 6:57am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Dear all
I try and finally come up with the solutions.
The code is as under:-
 
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100