VB.NET Ms Access: Menampilkan Data dari Database ke List Combobox


Sekarang kita coba menampilkan data dari tabel kategori ke dalam list combobox.
Atur layout sederhana seperti berikut:
Semudian source code di belakang form adalah sebagai berikut:
Imports System.Data.OleDb

Public Class Form1
  Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0" & _
                                 ";Data Source=latihan.accdb;" & _
                                 "Persist Security Info=False;")


  Private Sub Form1_Load(sender As Object, _
              e As EventArgs) Handles MyBase.Load

      Dim cmd As New OleDbCommand
     
Dim adapt As New OleDbDataAdapter
     
Dim dt As New DataTable

      Try
          con.Open()

          cmd.Connection = con
          cmd.CommandText = "Select kodekategori, namakategori From kategori"

          adapt.SelectCommand = cmd
          adapt.Fill(dt)


          ComboBox1.DataSource = dt
          ComboBox1.DisplayMember = "namakategori"
          ComboBox1.ValueMember = "kodekategori"

          con.Close()
      Catch myerror As OleDbException
          MessageBox.Show("Error: " & myerror.Message)
      Finally
          con.Dispose()
      End Try
  End Sub


End Class

Coba Run:
 List akan menampilkan data kategori.


Click here if you like this article.


Post a Comment

0 Comments