Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Created December 24, 2013 03:57
Show Gist options
  • Save hidayat365/8108624 to your computer and use it in GitHub Desktop.
Save hidayat365/8108624 to your computer and use it in GitHub Desktop.
Cara paling simple bin mudah untuk menampilkan isi combobox secara dinamis berdasarkan table yang ada di database menggunakan VB.net
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
InitializeComboBox()
End Sub
Private Sub InitializeComboBox()
' siapkan koneksi database
Dim cn As New SqlConnection(My.Settings.LocalDatabase)
' siapkan data adapter untuk data retrieval
Dim da As New SqlDataAdapter("select account_id, description from acc_accounts", cn)
' siapkan datatable untuk menampung data dari database
Dim dt As New DataTable
' enclose in try-catch block
' untuk menghindari crash jika terjadi kesalahan database
Try
' ambil data dari database
da.Fill(dt)
' bind data ke combobox
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "account_id"
ComboBox1.DisplayMember = "description"
' DONE!!!
Catch ex As Exception
' tampilkan pesan error
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment