dropdowncontrol problem

Discussion in 'ASP .Net' started by fredda054@hotmail.com, Jun 7, 2005.

  1. Guest

    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
    ==============================================================
     
    , Jun 7, 2005
    #1
    1. Advertising

  2. Phillip Ian Guest

    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.
     
    Phillip Ian, Jun 7, 2005
    #2
    1. Advertising


  3. > 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
     
    Eliyahu Goldin, Jun 7, 2005
    #3
  4. Phillip Ian Guest

    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.
     
    Phillip Ian, Jun 7, 2005
    #4
  5. Guest

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

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

    Fredrik
     
    , Jun 8, 2005
    #5
  6. 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
    >
    >
    >
     
    =?Utf-8?B?R2lsYmVydCBCaXJvIFNpbW9u?=, Aug 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,001
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,180
    Ted Miller
    Sep 13, 2003
  3. Bjorn Sagbakken

    BackgroundImage in a DropDownControl?

    Bjorn Sagbakken, Oct 24, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    261
    Bjorn Sagbakken
    Oct 24, 2007
  4. Luiz Vianna

    New DropDownControl behavior

    Luiz Vianna, Oct 2, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    101
    Luiz Vianna
    Oct 6, 2003
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    559
    Bullschmidt
    May 11, 2004
Loading...

Share This Page