Please i have a problem i cant resolve. I was able to populate my combobox (Cus_ID) from TLB_Customer table sucessfully. Now, how ca i use the same populated combobox to populate a textbox (Cus_Name) all in the same TLB_Customer table.

My Code

Imports System.Data.OleDb
Public Class frmTransaction
    Dim con As New OleDbConnection

    Private Sub frmTransaction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connection String
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\YHFarm.mdb"

        'Open Connection
        con.Open()

        'Referesh Controls
        RefreshControls()
    End Sub
    Public Sub RefreshControls()
        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\YHFarm.mdb"
        Dim cmd As New OleDbCommand
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "select Cus_ID from TLB_Customer"

        Dim dr As OleDbDataReader = cmd.ExecuteReader

        While dr.Read
            Cus_ID.Items.Add(dr.Item("Cus_ID"))
        End While
        dr.Close()
    End Sub
End Class

Recommended Answers

All 5 Replies

Are you trying to put the cust id in one control and cust name in another? That could get confusing to keep them straight. Much easier to put them in a grid control and let the grid control keep then together. And you can do it all in just one SQL statement. You can even bind the grid control to the database so that you don't have to loop through the result sets.

If you want to know how to do that, then see this excellent free tutorial

What you want is, if the user select cust_ID on combobox then it will assign the corresponding Cust_Name to Textbox?

Exactly ryan. Thats just what i want.
Once the form loads all customer ids will be populated into the combox. Now when you select any customer id, the customer name textbox is populated with the customer name of the corrsponding customer id selected from the combobox.
Thats all i need ASAP.
Thanks

Try this
on the click event of the combox
nametxtbox.text=combox.text
combox.text must be in a list box or a grid

Imports System.Data.OleDb
Public Class frmTransaction
    '//Declare some of the requirements for connecting to DB
    Dim CONNECTION_STRING As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\YHFarm.mdb"
    Dim con As OleDBConnection = New OleDBConnection(CONNECTION_STRING)
    Dim da As SqlDataAdapter
    Dim ds As DataSet = New DataSet

    Private Sub frmTransaction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '//Call the Connect method
        Connect()

        '//Loop the ds and add the Cust_ID to combobox
        Dim i As Integer
        For i = 0 To ds.Tables("CustomerTable").Rows.Count - 1
            Combobox.Items.Add(ds.Tables("CustomerTable").Rows(i).Item("Cust_ID").ToString)
        Next
    End Sub

    Private Sub product_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
        '//assign the corresponding customer base on the selected id to textbox       
        MessageBox.Show(ds.Tables("CustomerTable").Rows(ComboBox.SelectedIndex).Item("Cus_Name").ToString())
        Textbox1.text = ds.Tables("TECustomerTableST").Rows(ComboBox.SelectedIndex).Item("Cus_Name").ToString()
    End Sub

    '//Method for connecting to db and fills the dataset
    Private Sub Connect()
        Dim SQLText As String = "SELECT Cus_ID ,Cus_Name FROM CustomerTable"
        Try
            If con.State = ConnectionState.Open Then con.Close()
            con.Open()
            da = New OleDbDataAdapter(SQLText, con)
            da.Fill(ds, "CustomerTable")
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class

You have it. Try to study the code, and smile

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.