Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   dropdowncontrol problem (http://www.velocityreviews.com/forums/t104166-dropdowncontrol-problem.html)

fredda054@hotmail.com 06-07-2005 09:57 AM

dropdowncontrol problem
 
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
================================================== ============


Phillip Ian 06-07-2005 01:09 PM

Re: dropdowncontrol problem
 
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.


Eliyahu Goldin 06-07-2005 01:29 PM

Re: dropdowncontrol problem
 

> 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



Phillip Ian 06-07-2005 03:34 PM

Re: dropdowncontrol problem
 
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.


fredda054@hotmail.com 06-08-2005 02:41 AM

Re: dropdowncontrol problem
 
Wow, that was easy, and pretty obvious now that I see it !

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

Fredrik


=?Utf-8?B?R2lsYmVydCBCaXJvIFNpbW9u?= 08-13-2005 05:37 PM

Re: dropdowncontrol problem
 
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.

"Eliyahu Goldin" wrote:

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



All times are GMT. The time now is 08:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.