Results 1 to 2 of 2

Thread: VB2010 and Mysql does not display all data from table fields on my textboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Question VB2010 and Mysql does not display all data from table fields on my textboxes

    I have a MySQL Database (ixdatabase) where I have several tables one of them is "masterlist". I have several textboxes which can display the data from my table BUT not all. It can only display SURNAME,GIVEN,MID, MIDDLE but for the other fields, it can't. I have double checked my database and I'm positive that I am linking my code to the correct one, also there are no whitespaces or special characters. I am quite a newbie on this, I wonder what is wrong with what I am doing?

    Code:
    Imports MySql.Data.MySqlClient
     
    Public Class frm_masterlist
        Dim ServerString As String = "Server=localhost;User Id=root;Password=aaaaaa;Database=ixdatabase"
        Dim SQLConnection As MySqlConnection = New MySqlConnection
        Dim cmd As MySqlCommand = New MySqlCommand
        Dim dadapter As New MySqlDataAdapter
        Dim datardr As MySqlDataReader
        Dim strsql As String
     
        Private Sub frm_masterlist_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            SQLConnection.ConnectionString = ServerString
     
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Connection to Database is Established")
            Else
                MsgBox("Cannot establish connection to Database")
            End If
     
            strsql = "SELECT * FROM ixdatabase.masterlist ORDER BY SURNAME"
     
            cmd.CommandText = strSql
            cmd.Connection = SQLConnection
            dadapter.SelectCommand = cmd
     
            datardr = cmd.ExecuteReader
     
            If datardr.HasRows Then
                datardr.Read()
                tb_lname.Text = datardr("SURNAME")
                tb_fname.Text = datardr("GIVEN")
                tb_mname.Text = datardr("MID")
                tb_mi.Text = datardr("MIDDLE")
                tb_app.Text = datardr("APPELLATION")
                tb_prefix.Text = datardr("PREFIX")
                tb_sex.Text = datardr("SEX")
                tb_status.Text = datardr("STATUS")
            End If
     
        End Sub
     
        Private Sub cmd_close_Click(sender As System.Object, e As System.EventArgs) Handles cmd_close.Click
            Me.Close()
            SQLConnection.Close()
        End Sub
    End Class
    Or perharps, you can help me improve my code.. Please help. Thanks!

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB2010 and Mysql does not display all data from table fields on my textboxes

    I would suggest not using SELECT * but SELECT fields you want then data bind to your text boxes.

    The following example is OleDb but MySQL provider works the same. I don't have a MySQL database to provide you with an exact demo.

    Load data from database
    Code:
    Private Function Load_Customers() As DataTable
        Using cn As New OleDb.OleDbConnection With {.ConnectionString = "Your connection string"}
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                cmd.CommandText = "SELECT CompanyName, ContactName, ContactTitle, Identifier FROM Customer"
                Dim dt As New DataTable
                cn.Open()
                dt.Load(cmd.ExecuteReader)
                Return dt
            End Using
        End Using
    End Function
    In this example I have a BindingNavigator which allow the user to traverse all rows in the underlying DataTable of the BindingSource bsCustomers. Then bind a column in the DataTable named CompanyName to TextBox txtCompanyName.

    Code:
    Public Class frmMainForm
    
        WithEvents bsCustomers As New BindingSource
        Private Sub frmMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            bsCustomers.DataSource = Load_Customers()
            BindingNavigator1.BindingSource = bsCustomers
            txtCompanyName.DataBindings.Add("Text", bsCustomers, "CompanyName")
        End Sub
    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width