0

I have a problem with this exception in my website itemImage1 has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value.

To summarize the code, I have two radio buttons called guitar and bass. If the admin select guitar, it will then generate the specified file path and look for the images within the location. It will then display the available images in the dropdownlist. Same goes for bass but with a different location.

My first try with the code, as I select the guitar radiobutton, it worked perfectly and displays the available images in the dropdownlist. But when I change to the bass radio button, it is giving me the exception that I have mentioned above.

I have tried unbinding it by setting the datasource and selectedvalue to null, clearing the items and setting the SelectedIndex to -1, but it still isn't working. Where am I going wrong?

Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
    ShowItemImages();
}

private void ShowItemImages()
{
    var imageList = new ArrayList();
    if (itemType1.Checked)
    {
        itemImage1.Items.Clear();
        itemImage2.Items.Clear();
        itemImage1.SelectedIndex = -1;
        itemImage2.SelectedIndex = -1;
        itemImage1.SelectedValue = null;
        itemImage2.SelectedValue = null;
        itemImage1.DataSource = null;
        itemImage1.DataBind();
        itemImage2.DataSource = null;
        itemImage2.DataBind();
        itemselectedValueGuitar1 = itemImage1.SelectedValue;
        itemselectedValueGuitar2 = itemImage2.SelectedValue;
        var images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/"));
        foreach (var image in images)
        {
            var imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }
        itemImage1.DataSource = imageList;
        itemImage1.DataBind();
        itemImage2.DataSource = imageList;
        itemImage2.DataBind();
        itemImage1.SelectedValue = itemselectedValueGuitar1;
        itemImage2.SelectedValue = itemselectedValueGuitar2;
    }
    else if (itemType2.Checked)
    {
        itemImage1.Items.Clear();
        itemImage2.Items.Clear();
        itemImage1.SelectedIndex = -1;
        itemImage2.SelectedIndex = -1;
        itemImage1.SelectedValue = null;
        itemImage2.SelectedValue = null;
        itemImage1.DataSource = null;
        itemImage1.DataBind();
        itemImage2.DataSource = null;
        itemImage2.DataBind();
        itemselectedValueBass1 = itemImage1.SelectedValue;
        itemselectedValueBass2 = itemImage2.SelectedValue;
        var images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Bass/"));
        foreach (var image in images)
        {
            var imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }
        itemImage1.DataSource = imageList;
        itemImage1.DataBind();
        itemImage2.DataSource = imageList;
        itemImage2.DataBind();
        itemImage1.SelectedValue = itemselectedValueBass1;
        itemImage2.SelectedValue = itemselectedValueBass2;
    }
}

Here is the aspx code:

 <tr>
     <td style="width: 160px; height: 37px;"><strong>Item Type:</strong></td>
     <td style="height: 37px">
         <asp:RadioButton ID="itemType1" runat="server" Text="Guitar" AutoPostBack="True" GroupName="ItemType"/>
         <asp:RadioButton ID="itemType2" runat="server" Text="Bass" AutoPostBack="True" GroupName="ItemType"/>
     </td>
 </tr>
 <tr>
     <td style="width: 160px; height: 37px;">
         <strong>Item Image1:</strong></td>
         <td style="height: 37px">
         <asp:DropDownList ID="itemImage1" runat="server" Width="300px">
         </asp:DropDownList>
         <br />
         <asp:FileUpload ID="itemFileUpload1" runat="server" />
         <asp:Button ID="itemUploadImage1" runat="server" Text="Upload Image" OnClick="itemUploadImage1_Click"/>
     </td>
 </tr>
 <tr>
     <td style="width: 160px; height: 37px;">
         <strong>Item Image2:</strong></td>
         <td style="height: 37px">
         <asp:DropDownList ID="itemImage2" runat="server" Width="300px"></asp:DropDownList>
         <br />
         <asp:FileUpload ID="itemFileUpload2" runat="server" />
         <asp:Button ID="itemUploadImage2" runat="server" Text="Upload Image" OnClick="itemUploadImage2_Click"/>
     </td>
</tr>
Paul
  • 4,160
  • 3
  • 30
  • 56
Boom Click
  • 51
  • 2
  • 10
  • This is not a very succinct question... – IronAces Aug 10 '17 at 13:09
  • please be specific –  Aug 10 '17 at 13:11
  • Possible duplicate of [has a SelectedValue which is invalid because it does not exist in the list of items. How do you debug?](https://stackoverflow.com/questions/14654220/has-a-selectedvalue-which-is-invalid-because-it-does-not-exist-in-the-list-of-it) – VDWWD Aug 10 '17 at 13:15
  • 1
    This question has been asked [many times before](https://www.google.nl/search?site=&source=hp&q=has+a+SelectedValue+which+is+invalid+because+site%3Astackoverflow.com&oq=has+a+SelectedValue+which+is+invalid+because+site%3Astackoverflow.com&gs_l=psy-ab.3...365.8018.0.8319.27.26.1.0.0.0.147.1909.18j7.25.0....0...1.1.64.psy-ab..1.5.455...0j0i7i30i19k1j0i7i10i30i19k1j0i22i30k1j0i13k1j0i13i30k1j33i160k1.TzQ6O9gs-CQ). You are trying to set a `SelectedValue` that does not exists, as the error message indicates. – VDWWD Aug 10 '17 at 13:17
  • Yes already asked but there are no solutions yet. Ive already tried my solutions but none is working. – Boom Click Aug 10 '17 at 15:38
  • I have checked out the link you provided and i have already tried all suggested solutions in there. Haven't you seen the code that i post above? – Boom Click Aug 10 '17 at 15:43
  • Whats not so specific about this? I have already included the code above. That is literally the only code i have. – Boom Click Aug 10 '17 at 15:47

0 Answers0