Add Items To Combobox From Database In Visual Basic 2010 (VB.Net 2010)

This is tutorial how to add items to Combobox from Database in Visual Basic 2010 (VB.Net 2010). Easy way binding datasource to Combobox.

combox vb.net

SOURCE CODE


Imports System.Data.SqlClient

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'CREATE OBJECT CONNECTION
        Dim cn As New SqlConnection

        With cn
            .ConnectionString = "server=(local)\sqlexpress;database=trial;integrated security=true"
            .Open()
        End With

        'CREATE OBJECT DATA TABLE AND DATA ADAPTER
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter("select kode,nama from warna order by kode", cn)

        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            With ComboBox1
                .Items.Clear() 'CLEAR ITEMS COMBOBOX
                For i As Integer = 0 To dt.Rows.Count - 1
                    'ADD ITEMS TO COMBOBOX
                    .Items.Add(dt.Rows(i).Item("kode") & " - " & dt.Rows(i).Item("nama"))
                Next
                'CLEAR TEXT COMBOBOX
                .Text = Nothing
                .SelectedIndex = -1
            End With
        End If

        'CLEAR ALL OBJECT AND MEMORY
        With cn
            .Close()
            .Dispose()
        End With
        cn = Nothing
        dt.Dispose()
        dt = Nothing
        da.Dispose()
        da = Nothing

    End Sub

End Class


Video Tutorial

https://www.youtube.com/watch?v=klASZPDHtZs

Anda baru saja membaca artikel yang berkategori Combobox / Database / VB.Net dengan judul Add Items To Combobox From Database In Visual Basic 2010 (VB.Net 2010). Anda bisa bookmark halaman ini dengan URL http://vbcodesblog.blogspot.com/2014/10/add-items-to-combobox-from-database.html. Terima kasih!
Ditulis oleh: Farray Cool - Thursday, October 16, 2014

2 komentar untuk "Add Items To Combobox From Database In Visual Basic 2010 (VB.Net 2010)"

  1. terima kasih pa,

    scripnya suda saya ikuti dan berhasil

    ReplyDelete
  2. satu lagi pa,

    kalau saya klik combobox dua colom menjadi satu.
    saat diklik hanya kolom satu atau kolom dua yang tampil di combobox. bagian mana scrip yg harus di tambah?

    makasih
    windi

    ReplyDelete