Monday 27 February 2012

Fill Combobox using dataset in VB.net


                         If You have a combobox1 in a form (“Form 1”) and a database in SQL server  with a table,  named “tblCountry”  having  fields “countryID”, ”countryName” with data then  you can strat from here..... :)
Because, we fill combobox with Country with its id,


For database connection using dsn

We are using odbc connection , So we must import odbc class file first into the form(“Form 1”),

Imports System.Data.Odbc

Create a sub function in your form

Public Sub FillMaComboBox()

        Dim sqlQury As String
        sqlQury = "select countryID, countryName from tblCountry" 

        Dim ds As New DataSet
        Dim odbda As New OdbcDataAdapter(sqlQury, conDb) ‘conDb is connection object
        odbda.Fill(ds) ‘Fill selected data to ds(our dataset) using OdbcDataAdapter

        ComboBox1.DataSource = ds.Tables(0).DefaultView
        ComboBox1.DisplayMember = “countryName” ‘We want to display country
        ComboBox1.ValueMember = “countryID” ‘We store id as valueMember of ComboBox

End Sub



Now you did it bro.,
You can use this “ FillMaComboBox() into any event handler that you want….:)




No comments:

Post a Comment