By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

combo value validation

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 19 '05 #2

P: n/a
try this, it never errors out for bad values

ddUserProg1.SelectedIndex =
ddUserProg1.Items.IndexOf(ddUserProg1.Items.FindBy Value(csUser.ProgramID))

vinay
"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

Nov 19 '05 #3

P: n/a
Thank You!
-----Original Message-----
try this, it never errors out for bad values

ddUserProg1.SelectedIndex =
ddUserProg1.Items.IndexOf(ddUserProg1.Items.FindB yValue (csUser.ProgramID))
vinay
"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

.

Nov 19 '05 #4

P: n/a

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 wayof 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

.

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.