combo value validation

B

briandh

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
 
G

Guest

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!
***************************
 
G

Guest

try this, it never errors out for bad values

ddUserProg1.SelectedIndex =
ddUserProg1.Items.IndexOf(ddUserProg1.Items.FindByValue(csUser.ProgramID))

vinay
 
B

briandh

Thank You!
-----Original Message-----
try this, it never errors out for bad values

ddUserProg1.SelectedIndex =
ddUserProg1.Items.IndexOf(ddUserProg1.Items.FindByValue (csUser.ProgramID))

vinay



.
 
B

briandh

Thank You!

-----Original Message-----
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!
***************************


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top