how to store all items of database to combo box.
i.e. store complete column in combo box?

Recommended Answers

All 7 Replies

Like:

Using conn As New SqlConnection("connString")
    Using cmd As SqlCommand = New SqlComand("SELECT ColumnName FROM TableName", conn)
        Using reader As SqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                comboBox1.Items.Add(reader(0).ToString())
            End While
        End Using
    End Using
End Using

Hi,

A couple of ways, try this for starters:

dim DR as SQLDataReader
dim cmd as SQLCommand

cmd = New SqlCommand("SELECT Column FROM table", DBconnection)
DBconnection.Open()
DR = cmd.ExecuteReader()
If DR.HasRows Then
        Do While DR.Read()
            MyComboBox.Items.Add(DR.GetString(0))
        Loop

end if
DR.Close()

G_Waddell

Should declare DBconeection as what type?
Mycombobox is name of the combo box?

Ok I got dbconnection is connection string.but what is mycombobox?

G_Waddell

G_Waddell
Ur code din't work out

Mitja Bonca
Even ur code din't work out.

Sure it does, you must have the correct connection string of your database. Are you sure you have one?
Which is exactly your database?

To answer on your last question, myComboBox is a reference (a reference name) to your ComboBox control.
When you create (instanitate) new comboBox control you do:
ComboBox comboBox1 = new ComboBox(); //is this example comboBox1 is same as myComboBox - its just a variable - name

But this code does C# automatically by it self when you add this control to the form. If you will look into Desiger of your form, you will see this code is there. And much more (like Size, Location, Index,...)

Hope it help explaining initial issues.

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.