Question how to get selected value combobox

gecko123

New member
Joined
Nov 15, 2010
Messages
1
Programming Experience
Beginner
hello, i have a problem with combobox,
this is my code.

VB.NET:
Private Sub LoadDept()
        conn = New SqlConnection("Data Source=EXCO-2B41B5;Initial Catalog=payroll;Integrated Security=True")
        cmdUser = conn.CreateCommand
        cmdUser.CommandText = "select * from department order by number asc"
        daUser.SelectCommand = cmdUser
        daUser.Fill(dsUser, "department")
        dtUser = dsUser.Tables("department")
        For i = 0 To dtUser.Rows.Count - 1
            cmbDept.Items.Add("" & dtUser.Rows(i).Item(0) & "/" & dtUser.Rows(i).Item(1) & "")
        Next i
        conn.Close()
End Sub

dtUser.Rows(i).Item(0) = first field in my table is "number"
dtUser.Rows(i).Item(1) = second field "division"

result in combobox
1/Injection
2/Warehouse
3/Maintenance

my question is how to insert into database, but the value it just the Item(0) "number" . not both.

Any kind of help would be appreciated.
 
Don't populate the ComboBox like that. Use data-binding:
With cmbDept
    .DisplayMember = "division"
    .ValueMember = "number"
    .DataSource = dtUser
End With
Now, the user will select a division from the list and you get the corresponding number from the SelectedValue property.
 
Back
Top