I'm trying to set the selected value from a combo box that is populated from my database,
i cant seem to find a way to do that.

I'm using this to populate my combo box:
Private Sub PopulateCountry()
Dim country As New Client
Dim contryList As OdbcDataReader = country.mpopulateCountry()
Dim objCountry As String
If contryList.HasRows Then
contryList.Read()
Do While contryList.Read
objCountry = contryList(0)
cbxCCountry.Items.Add(objCountry)
cbxCountry.Items.Add(objCountry)
Loop
End If
End Sub

And calling this method(PopulateCountry()) in the form load and then using this to set the value upon my button btnAddToDB:
Dim country As String
country = cbxCCountry.SelectedValue

Any suggestion guys?

This is how I did it-

Public Class Form4

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cn As New Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=xxxxx;")
        Dim cmd As New Odbc.OdbcCommand("Select * from mytable", cn)
        cn.Open()
        Dim dr As Odbc.OdbcDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            Me.ComboBox1.Items.Add(dr(0))
            ' dr(i) where i is the column cardinal
            ' if the 2nd field is needed it should be dr(1)

        End While
        dr.Close()
    End Sub
End Class

just solved it. my code works i just got mixed up with the ".SelectedItem" and ".SelectedValue". :D

hi jalandoonk,

can u give me the code for
"-- how to set the selected value of Combobox..--"

I hav been struggling with it since past 4days..

Thankyou
venu... venu_hm@hotmail.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.