'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive (part 2)

Discussion in 'ASP .Net' started by Stimp, Nov 8, 2005.

  1. Stimp

    Stimp Guest

    Hi all,

    I've come back to this problem again and I've identified which part of
    my code is producing the error:

    "'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive"


    I have a dropdown 'Year' and it contains a list of years from 2000 to
    2005. This dropdown is populated on page load.

    When I select an article to edit (which has a year value associated), I
    select the appropriate year dropdown value as follows...

    ddYear.SelectedValue = objRS("VYear")


    This is causing the above error, I THINK it's because the 'VYear' value
    is an integer as opposed to a string.

    I've also tried: ddYear.SelectedValue = CInt(objRS("VYear"))
    and ddYear.SelectedValue = CStr(objRS("VYear"))

    with the same error resulting.

    Any ideas?

    Thanks.
    Peter
    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Nov 8, 2005
    #1
    1. Advertising

  2. Stimp

    Greg Burns Guest

    Does this work?

    Dim li As ListItem = ddYear.Items.FindByValue(CStr(objRS("VYear")))
    If Not li Is Nothing Then
    li.Selected = True
    End If

    Greg


    "Stimp" <> wrote in message
    news:...
    > Hi all,
    >
    > I've come back to this problem again and I've identified which part of
    > my code is producing the error:
    >
    > "'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive"
    >
    >
    > I have a dropdown 'Year' and it contains a list of years from 2000 to
    > 2005. This dropdown is populated on page load.
    >
    > When I select an article to edit (which has a year value associated), I
    > select the appropriate year dropdown value as follows...
    >
    > ddYear.SelectedValue = objRS("VYear")
    >
    >
    > This is causing the above error, I THINK it's because the 'VYear' value
    > is an integer as opposed to a string.
    >
    > I've also tried: ddYear.SelectedValue = CInt(objRS("VYear"))
    > and ddYear.SelectedValue = CStr(objRS("VYear"))
    >
    > with the same error resulting.
    >
    > Any ideas?
    >
    > Thanks.
    > Peter
    > --
    >
    > "I hear ma train a comin'
    > ... hear freedom comin"
    >
     
    Greg Burns, Nov 8, 2005
    #2
    1. Advertising

  3. Stimp

    Stimp Guest

    On Tue, 8 Nov 2005 Greg Burns <> wrote:
    > Does this work?
    >
    > Dim li As ListItem = ddYear.Items.FindByValue(CStr(objRS("VYear")))
    > If Not li Is Nothing Then
    > li.Selected = True
    > End If
    >
    > Greg


    doesn't seem to... is there a way to set the SelectedIndex instead of
    the SelectedValue if all I have is the text value?

    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Nov 8, 2005
    #3
  4. Stimp

    Greg Burns Guest

    Put a breakpoint on "li.Selected = True". Does the debugger ever even get
    there? If not, then it is not even finding that value in your current
    listitems, hence cannot select it.


    "Stimp" <> wrote in message
    news:...
    > On Tue, 8 Nov 2005 Greg Burns <> wrote:
    >> Does this work?
    >>
    >> Dim li As ListItem = ddYear.Items.FindByValue(CStr(objRS("VYear")))
    >> If Not li Is Nothing Then
    >> li.Selected = True
    >> End If
    >>
    >> Greg

    >
    > doesn't seem to... is there a way to set the SelectedIndex instead of
    > the SelectedValue if all I have is the text value?
    >
    > --
    >
    > "I hear ma train a comin'
    > ... hear freedom comin"
    >
     
    Greg Burns, Nov 8, 2005
    #4
  5. Stimp

    Stimp Guest

    On Tue, 8 Nov 2005 Greg Burns <> wrote:
    > Put a breakpoint on "li.Selected = True". Does the debugger ever even get
    > there? If not, then it is not even finding that value in your current
    > listitems, hence cannot select it.


    my mistake, it DOES indeed work.. the new error I was receiving was
    separate to the original problem, which your code fixed.

    Thanks!

    It's still very unusual that you need to use this method to set the
    selectedvalue when the text field in your dropdownlist is numerical
    *shrug*
    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Nov 8, 2005
    #5
  6. Stimp

    Greg Burns Guest

    Not sure I understand, but the Value and Text property of you ListItem are
    both strings. You have to convert whatever object you want to be a ListItem
    into a string to add it to the collection.

    ddYear.Items.Insert(0, New ListItem("displayedtext", "actualvalue"))

    This code insert in position 0 a new new ListItem. The constructor for
    ListItem only takes strings.

    To tell you the truth, I am not sure why you are having a problem with:

    ddYear.SelectedValue = objRS("VYear").ToString

    Greg


    "Stimp" <> wrote in message
    news:...
    > On Tue, 8 Nov 2005 Greg Burns <> wrote:
    >> Put a breakpoint on "li.Selected = True". Does the debugger ever even
    >> get
    >> there? If not, then it is not even finding that value in your current
    >> listitems, hence cannot select it.

    >
    > my mistake, it DOES indeed work.. the new error I was receiving was
    > separate to the original problem, which your code fixed.
    >
    > Thanks!
    >
    > It's still very unusual that you need to use this method to set the
    > selectedvalue when the text field in your dropdownlist is numerical
    > *shrug*
    > --
    >
    > "I hear ma train a comin'
    > ... hear freedom comin"
    >
     
    Greg Burns, Nov 8, 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. ALuPin

    Mutually exclusive

    ALuPin, Sep 7, 2004, in forum: VHDL
    Replies:
    2
    Views:
    754
    Mike Treseler
    Sep 7, 2004
  2. js
    Replies:
    2
    Views:
    912
  3. Stimp
    Replies:
    2
    Views:
    813
    Stimp
    Oct 24, 2005
  4. Replies:
    0
    Views:
    636
  5. Dooman
    Replies:
    6
    Views:
    9,246
    rabiniak.piotr
    Mar 2, 2012
Loading...

Share This Page