Wednesday, March 21, 2012

populating a drop-down problem

Hello - I have a dropdown of US States that I populate from an xml file.

I then have a user log in to my site and their details are pulled off a database - including their address with state code.

I then set the selected value of the drop down to the users state.

The problem I have is if the sate on the database is empty or incorrect for some reason. When I set the selected value of the drop down list, I get an error because I'm trying to set it to a value that isn't there.

How do I get around this?

I need some sort of way to say - select this value from the dropdown only if the value exists - without giveing the error.

Any ideas?

ThanksmyComboBox.Items.Count will give you the number of items in the combo. If the number is not 0, you can use the SelectedValue, otherwise not.
That's not quite the problem but thanks anyway - what I mean is:

Lets say I have a drop down list with the values of

NY
CY
FL
NJ

and then I use dropdownList1.selectedValue = "DFGDF"

It's not going to find that value right?

What I wanted to know was is it possible to find out before I specify the selected value whether the value is actually in the list or not.

I have found a way round it though using:

Try
dropDownStates.SelectedValue = usrRow.Item("State")
Catch ex As Exception

End Try

At least that way I don't get an error but I was wondering if there's a way of telling what values are in the dropdown before specifying which is selected..

Thanks anyway.
Why not populate the ddl from the database and then you are sure that there is an entry for every possible selected item?

HTH
Well - the list of states is correct in that all the codes are correct and all the states are there. I need to provide all the states in the drop down to allow new users to sign up and for existing users to pick a new state if they move house.

The problem I have is that the data I'm initialy supllied with probably wont be perfect. I have no control over this data either - I just get the data and have to eork with it as it is. So I have to account for some of the users on the datanase not having the correct state code. Some might not have a code at all and others may be 'New York' rather then NY..

I suppose a way round it would be to:

create a dataset from the xml file of states

cerate a dataView from the dataset and see if the users state (NY for eaxmple) was in there.

if it was, I could then set the selected value of the dropdown to that value otherwise I would just leave it.

That just sounds like a long way of doing it though.

Isn't there some property of the dropdown like

dropdownlist.containsvalue ?

That would make it much easier.

0 comments:

Post a Comment