There are quite a few ways I can think of to start coding this.
1. Try ... Catch
Try
cboEmployeeID.SelectedValue = Context.Items("ID")
Catch
'This is just here to catch the error
End Try
2. Loop through controls collection, looking for this value
Dim cc As ControlsCollection = MyDropDown.Controls
For i = 0 to (cc.Count - 1)
'Loop looking for value
Next
3. Get ControlsCollection and see if it contains a specific key
If (cc.Contains(stringValue)) Then
'Code here to set value
End If
There are probably others. I am most fond of the third value, with some way
of informing the user they typed in something invalid in an Else Condition.
---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"briandh" wrote:
Hi
I have a web form, using vb.net code behind, with a combo
box populated with a dataset (Name, ID). What I have
found, unlike a windows form, is that if I try to pass a
value (ID) into the combo and that value is not part of
the dataset, the web form blows up.
If (Context.Items("ID") <> "") Then
cboEmployeeID.SelectedValue = Context.Items("ID")
End If
Testing for empty string does help some but there are
times when we have bad data and the ID value will not
match any in the dataset.
Is there a way to trap this so my form does not error out?
Thanks
Brian