Link to home
Start Free TrialLog in
Avatar of holemania
holemania

asked on

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

SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of holemania
holemania

ASKER

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.
Thanks for the feedback guys.  Decided to take it the other way and just validate to make sure that the ID is correct.
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.