Today we will learn how to populate ComboBox from SQLite database using Vb.Net.
Step 1: Create VS project.
Step 2: Add Combobox to the form.
Step 3: Create LoadCustomers function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Private Function LoadCustomers() As DataTable Dim dtCustomer As New DataTable Dim mSQL As String = "SELECT * FROM vCustomers" Dim connectionString As String = "Data Source=Chinook.db;Version=3;" Try Using conn As New SQLiteConnection(connectionString) Using cmd As New SQLiteCommand(mSQL, conn) conn.Open() Using adapter As New SQLiteDataAdapter(cmd) adapter.Fill(dtCustomer) End Using End Using End Using Catch ex As Exception End Try Return dtCustomer End Function |
Step 4: Update the following property of the combobox in the Form Load.
1 2 3 4 5 6 7 8 9 10 |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim dt As New DataTable dt = LoadCustomers() comboCustomers.DataSource = dt comboCustomers.DisplayMember = "CustomerName" comboCustomers.ValueMember = "CustomerId" End Sub |