How you can use a Combobox and a Listbox and populate them from MYSQL Database in VB.Net
In this article I am going to show you how you can use a Combobox and Listbox to select a value from the Listbox as well as from the ComboBox and populate them from MYSQL Database table in Visual Basic.Net. The values which we select are actually being populated from the database table.
In the previous article I show you how to create a Text Editor in Visual Basic.Net.
In this article I am going to show you how you can use a Combobox and a Listbox and populate them from MYSQL Database table in Visual Basic.Net.
Since a Combobox in Visual Basic.Net is used to display data in a drop down box. The combo box has actually two parts i,e a Text Box and a list box. A user can select an item from the list as well as can type the item in the text box.
Some natable properties of a combobox are: Text,Name,Sorted etc
A Listbox displays a list of items from which a user can select one or more items from the list. If there are many items in the listbox scrollbars do appear automatically and lets the user to scroll through the list.
Some notable properties of the Listbox are: Name, Sorted,Multicolumn etc
First we design an interface in VB.Net form as shown in the picture.
Now create a table in MYSQL database and then use the following code to populate the ListBox and ComboBox from the database Table on the load event of the form and select the items accordingly.
In the code window of the form write the code as under:
Imports MySql.Data.MySqlClient
Public Class myclass
Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=students;user id=root;password=")
Private Sub login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim da As New MySqlDataAdapter("select * from users", con)
Dim mytable As New DataTable()
da.Fill(mytable)
ComboBox1.DataSource = New BindingSource(mytable, Nothing)
ComboBox1.DisplayMember = "stname"
ListBox1.DataSource = New BindingSource(mytable, Nothing)
ListBox1.DisplayMember = "stname"
End Sub
End Class