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?
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the feedback guys. Decided to take it the other way and just validate to make sure that the ID is correct.
ASKER
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.
ASKER