Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I HAVE A FORM WHERE I WANT TO RETRIEVE DATA FROM A TABLE WHERE TWO COLUMNS OF CODE AND SUBJECTS. THE FORM HAS A COMBOBOX AND TEXTBOX.WHEN I SELECT THE CODE FROM THE COMBOBOX THEN THE CORRECT SUBJECT IS DISPLAYED ON THE TEXTBOX

What I have tried:

VB



DIM CON AS OLEDBCOMMAND=NEW OLEDBCOMMAND("PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=D;
CON.OPEN
DIM CMD AS OLEDBCOMMAND=NEW OLEDBCOMMAND("SELECT FROM SUBJECTS", CON)
DR=CMD.EXECUTE READER()
WHILE DR.READ=TRUE
CMBSUBJECT.ITEMS.ADD(DR(0))

TXTSUBJECT.TEXT=DR.GETVALUE(1)
END WHILE
Posted
Updated 27-Aug-18 2:09am
Comments
Richard MacCutchan 27-Aug-18 5:22am    
You are setting the textbox from the first combobox entry. You need a combobox event hander to capture the selectionchanged event.

BTW: your caps lock button seems to be stuck.
Nelek 27-Aug-18 7:16am    
In addition I miss a (") in your code.

1 solution

Just try with below example

Dim dt As New DataTable
        dt.Columns.Add("Id", GetType(Int32))
        dt.Columns.Add("Data", GetType(String))
        dt.Rows.Add(1, "Test1")
        dt.Rows.Add(2, "Test2")
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "Data"
        ComboBox1.ValueMember = "Id"



Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
       If Not IsNothing(ComboBox1.SelectedItem) Then
           If Not IsNothing(ComboBox1.SelectedValue) Then
               txtValueTxt.Text = ComboBox1.SelectedValue
           End If

           txtDisplayText.Text = DirectCast(ComboBox1.SelectedItem, System.Data.DataRowView).Row.ItemArray(1)

       End If

   End Sub


don't forget to accept answer if you got solution.
 
Share this answer
 
v2

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