VB.NET - Combo box can't set default

I'm running into an issue where on initial load of my windows form, I would populate a combobox.  Base on selection, I want that combo box to change, but for some reason if it's property is listed as "DropDownList", it won't change.

My first code populate the combobox with IDs on initial form load.  If a department is fill in, it'll automatically select the right ID.  It's not doing that if I set it to dropdownlist.  If I just set it to dropdown, it works fine.  Any reason why?
'On form load:

Sub GetEmpID()

strQuery = "Select distinct ID from Employee"

Dim con String = "data source=SVR1;persist security info=False;initial catalog=db1;UID=sa;PWD=Password1"

Dim da as New SqlClient.SqlDataAdapter(strQuery, con)

Dim ds as Dataset = New Dataset

da.fill(ds)
cboEmpID.Datasource = ds.Tables(0)
cboEmpID.DisplayMember = ds.Tables(0).Columns("ID").ColumnName.ToString
cboEmpID.ValueMember = "ID"

End Sub


'On text change from another location:


Sub AutoFillEmpID()

Dim strConnect as String = "data source=SVR1;persist security info=False;initial catalog=db1;UID=sa;PWD=Password1"

Dim strEmpQuery as String = ("Select distinct ID from employee where Dept = '" & txtDept.text & "' ")

Dim con as New sqlclient.sqlconnection(strConnect)
Dim cmd as New sqlclient.sqlcommand(strEmpQuery, con)

con.Open()

drEmployee = con.ExecuteReader(CommandBehavior.CloseConnection)

drEmployee.Read()

If drEmployee.HasRows Then
    cboEmpID.Text = drEmployee.Item("ID").ToString
Else
    Exit Sub
End if
End Sub

Open in new window

holemaniaAsked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
0
 
CodeCruiserConnect With a Mentor Commented:
Try setting the SelectedValue instead of text.
0
 
holemaniaAuthor Commented:
Thanks, but still not working.  Just strange that for some department code it would select the right ID, and for some it doesn't do it unless I set the combobox to dropdown instead of dropdownlist.  For example, Eng would select the right ID, but Acct doesn't.  I can't see any difference between the 2 other than if i select the property of the combobox to dropdown, then it works fine.  However, I don't want a dropdown combobox since user can type in the ID in there and then I would have to do another validation to see if the ID is an actual ID and in the right department.
0
 
holemaniaAuthor Commented:
Thanks for the feedback guys.  Decided to take it the other way and just validate to make sure that the ID is correct.
0
 
holemaniaAuthor Commented:
Thanks for the suggestions and help.  Decided to do it differently and set the property to dropdown.  From there, I setup another routine to do a validation before saving/updating.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.