Question From combo box to texboxes

suryatiakz

Member
Joined
Oct 2, 2010
Messages
7
Programming Experience
Beginner
After display list of data in combo box, next i would like to choose the data from the combo box and display all the information from the chosen data to texboxes , the data from combo box was link from my database (ms access).
 

Attachments

  • combo box.jpg
    combo box.jpg
    67.8 KB · Views: 28
There are two basic options:

1. Retrieve all the data to begin with and bind it to all the controls, in which case everything will happen automatically from then on. Advantages include:

- no waiting when navigating
- never retrieve same data twice
- no code except to load and save data

2. Retrieve just the data to display in the ComboBox and then query the database each time a selection is made. Advantages include:

- faster startup, especially when total amount of data is large
- only retrieve the data you need

You need to decide which way you want to go first.
 
In that case, you need to retrieve at least the primary key column to populate the ComboBox, and the column you want to display if that's not the primary key. You would use a DataAdapter to populate a DataTable with that data, then bind it to the ComboBox something like this:
VB.NET:
myComboBox.DisplayMember = "Name"
myComboBox.ValueMember = "ID"
myComboBox.DataSource = myDataTable
You would then handle the SelectionChangeCommitted event of the ComboBox. You should read the MSDN documentation to find out the difference between that and the SelectedIndexChanged event. In the event handler, you would create a Command with the appropriate SQL code and set a parameter based on the ComboBox selection, e.g.
VB.NET:
Using connection As New SqlConnection("connection string here")
    Using command As New SqlCommand("SELECT * FROM MyTable WHERE ID = @ID", connection)
        command.Parameters.AddWithValue("@ID", CInt(myComboBox.SelectedValue))
You can either call ExecuteReader and populate the controls manually or you can bind a DataTable to the controls and clear and populate it each time using a DataAdapter.
 
Back
Top