Hi All,

I have a combo box in a form, I want to fill it with names I have stored in a database.

I know I have to use the fill command, but I am not sure what code I need to use.

I imagine I need to place this code in the form load event, where I wrote my code to connect to the database.

Tell me if I am wrong, but, do I need to create a sql fill command, select Names From Users, then execute that command in the combo box object.

Any help would be much appreciated.

Thanks

John

Hi!

>>>>I imagine I need to place this code in the form load event

It depends on your need when you want your combobox to fill. So, use appropriate event for that. Here is a Form_Load event sample code in a link that might help you:

http://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/8844df20-9c56-47b5-8023-3d5b38d454d2

Also these two links will help you to proceed further: (read the details and check the code present at the end)

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember.aspx

Edited 5 Years Ago by ShahanDev: :( was actually : ( which changed to smily

Hi!

>>>>I imagine I need to place this code in the form load event

It depends on your need when you want your combobox to fill. So, use appropriate event for that. Here is a Form_Load event sample code in a link that might help you:

http://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/8844df20-9c56-47b5-8023-3d5b38d454d2

Also these two links will help you to proceed further: (read the details and check the code present at the end)

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember.aspx

Hi,

Thanks for he help. I came up with the code below.

Private Sub LoginForm1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrmanager;Integrated Security=True")
        conn.Open()
        Dim dsUserName As New DataSet
        Dim UserName As SqlDataAdapter
        UserName = New SqlDataAdapter("select Name from Users", conn)
        Dim cmdBuilder As New SqlCommandBuilder(UserName)
        UserName.Fill(dsUserName, "Users")
        ComboBox1.DataSource = dsUserName.Tables("Users")
    End Sub

However when I run the app, the combobox shows:

"System.Data.dataRowView"

have I written the code wrong?

Thanks very much

John

Hi,

Thanks for he help. I came up with the code below.

Private Sub LoginForm1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrmanager;Integrated Security=True")
        conn.Open()
        Dim dsUserName As New DataSet
        Dim UserName As SqlDataAdapter
        UserName = New SqlDataAdapter("select Name from Users", conn)
        Dim cmdBuilder As New SqlCommandBuilder(UserName)
        UserName.Fill(dsUserName, "Users")
        ComboBox1.DataSource = dsUserName.Tables("Users")
    End Sub

However when I run the app, the combobox shows:

"System.Data.dataRowView"

have I written the code wrong?

Thanks very much

John

Problem sorted, please see working code below.

Private Sub LoginForm1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrmanager;Integrated Security=True")
        conn.Open()
        Dim dsUserName As New DataSet
        Dim UserName As SqlDataAdapter
        UserName = New SqlDataAdapter("select Name from Users", conn)
        Dim cmdBuilder As New SqlCommandBuilder(UserName)
        UserName.Fill(dsUserName, "Users")
        ComboBox1.DataSource = dsUserName.Tables("Users")
        ComboBox1.DisplayMember = "Name"
    End Sub

have a happy new year everyone :)

John

This question has already been answered. Start a new discussion instead.