Showing posts with label Combobox. Show all posts
Showing posts with label Combobox. Show all posts
Thursday, October 16, 2014

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

Farray Cool Combobox, Database, VB.Net