dropdowncontrol problem

F

fredda054

Hi everybody !

I have a little anoying problem with a drop down list in a web app.
the problem is:
I populate a dropdownlist, (ddlCountry), with a dataset containing data
from a db table, (countryID and countryName). to avoid the problem with
having the first item always selected i added a "default" item first by
using:

ddlCountry.Items.Insert(0, "-- select a country --")

everything is working just fine, except if someone for some reason
selects that default row again.

What I do is, I use the valuefield of the dropdownlist, (countryID)
which is an integer, and in the "ddlCountry_SelectedIndexChanged"
eventhandler I use this ID and pases it as a parameter to a function
that retrieves States from the database, based on this countryID. I
fill that info from the db into a dataset and use it to populate a
datagrid, which shows the states of that specific country.

If I select the default item I get the errormessage:
"Input string was not in a correct format"

I thought it would be enough to just make a check in the event handler,
and make sure to do the 'work' only if the value is > 0, but this wont
work for me...

I add the code below, as it was before that check...
could someone please help me to solve this? what check do I have to do
?
What I want is, if the "-- select a country --" is selected again,
nothing should happen. I'm sure this is pretty simple, I'm just to
tired right now see things clearly...

Thanks a lot !!!

Fredrik


The event handler code:
===========================================================
Private Sub ddlCountry_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlCountry.SelectedIndexChanged

Dim objStateBL As New StateBL

Dim CountryID As Integer = ddlCountry.SelectedItem.Value
'this is the line that causes the error
dsState = objStateBL.getStates(CountryID) 'fill the dataset
dgdState.DataSource = dsState
dgdState.DataBind()
dgdState.Visible = True
btnAdd.Visible = True
If dsState.Tables(0).Rows.Count = 0 Then
lblMessage.Visible = True
lblMessage.Text = "No states available for the moment."
End If

End Sub
==============================================================
 
P

Phillip Ian

I'm guessing that your inserted default doesn't have a "Value", so the
line that's erroring is trying to convert Nothing to an integer.

Try:

ddlCountry.Items.Insert(0, new ListItem("-- select a country --", 0))

That will set the Value property of the new ListItem to 0. Then you
just have to make sure getStates handles a 0 CountryID gracefully and
returns an empty dataset.
 
E

Eliyahu Goldin

I thought it would be enough to just make a check in the event handler,
and make sure to do the 'work' only if the value is > 0, but this wont
work for me...

Why? Just lookup up the help on ddl (freely available) and, if it still
doesn't work after that, debug it. That's what programming is all about.

Eliyahu
 
P

Phillip Ian

Sorry if this double-posts, but I didn't see it come through before.

Your problem is that the "default" ListItem you are adding doesn't have
a "value".

Try ddlCountry.Items.Insert(0, new ListItem("-- select a country --",
0))

Then make sure getStates returns an empty dataset when CountryID = 0,
and you're all set.
 
F

fredda054

Wow, that was easy, and pretty obvious now that I see it !

Thanks a lot man ! everything's running smothly now !

Fredrik
 
G

Guest

What, were you in a bad mood when you posted this reply? It gives absolutely
no help and shouldn't be included in a forum such as this.

Ususally, at least for me, I go to forums when all other venues of help have
been exhausted to no avail. I can't begin to tell you how disappointed I was
when I found an issue that relates to the one I'm having only to find someone
posted an answer such as yours.

If you were in a bad mood maybe you should put your keyboard out of reach
until you get over it.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top