Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm now learning all about asp.net and trying out its basic tools. I've created a simple form where the admin can upload images for the website. The problem is, it is giving me this error: 'itemImage1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value.

Now, to better understand what I'm trying to do. Here is an image of the form that i have created in my website.

(Please checkout the image here for the form in this link: )[^]

As you can see, The admin has an option whether he wants to upload an image for guitar or bass products. For this example, I selected the Guitar radiobutton. Then, it will generate the available images in the dropdownbox, under Item Image1 and Item Image2.

Now here is the problem, Let's say i made a mistake of selecting the Guitar button and I wanted to select the Bass button. So I basically just clicked the Bass button, and then it gave me the exception error that I have mentioned earlier.

Please help me on solving this one, I'm still a beginner in coding and I would love to learn from you guys. I will include the codes that i have written.

What I have tried:

Here is the code for the aspx.cs file:

C#
protected void Page_Load(object sender, EventArgs e)
{
    string itemselectedValue1 = itemImage1.SelectedValue;
    ShowItemImages1();
    itemImage1.SelectedValue = itemselectedValue1;

    string itemselectedValue2 = itemImage2.SelectedValue;
    ShowItemImages2();
    itemImage2.SelectedValue = itemselectedValue2;

}

private void ShowItemImages1()
{
    if (itemType1.Checked)
    {
        string[] images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/"));
        ArrayList imageList = new ArrayList();

        foreach (string image in images)
        {
            string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }

        itemImage1.DataSource = imageList;
        itemImage1.DataBind();

    }
    else if (itemType2.Checked)
    {

        string[] images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Bass/"));
        ArrayList imageList = new ArrayList();

        foreach (string image in images)
        {
            string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }

        itemImage1.DataSource = imageList;
        itemImage1.DataBind();

    }
}


private void ShowItemImages2()
{
    if (itemType1.Checked) {

        string[] images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/"));
        ArrayList imageList = new ArrayList();

        foreach (string image in images)
        {
            string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }

        itemImage2.DataSource = imageList;
        itemImage2.DataBind();

    } else if (itemType2.Checked) {

        string[] images = Directory.GetFiles(Server.MapPath("~/Images/Brands/String Instrument Items/Bass/"));
        ArrayList imageList = new ArrayList();

        foreach (string image in images)
        {
            string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
            imageList.Add(imageName);
        }

        itemImage2.DataSource = imageList;
        itemImage2.DataBind();

    } 
}


Here is also the aspx file(if needed):

C#
<table cellspacing="15" class="brandsTable">

               <tr>
                    <td style="width: 160px; height: 37px;">

                    Item Type:</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;">

                    Item Brand:</td>

                    <td style="height: 37px">

                    <asp:TextBox ID="itemBrand" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td style="width: 160px; height: 37px;">

                    Item Model:</td>

                    <td style="height: 37px">

                    <asp:TextBox ID="itemModel" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td style="width: 160px; height: 37px;">

                    Item Price:</td>

                    <td style="height: 37px">

                    <asp:TextBox ID="itemPrice" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td style="width: 160px; height: 37px;">

                    Item Image1:</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;">

                    Item Image2:</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>


Here is also the source error and stack trace:
C#
Source Error: 


Line 20:             itemselectedValue1 = itemImage1.SelectedValue;
Line 21:             ShowItemImages1();
Line 22:             itemImage1.SelectedValue = itemselectedValue1;
Line 23: 
Line 24:             itemselectedValue2 = itemImage2.SelectedValue;

Source File: c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\Pages\CreateBrands.aspx.cs    Line: 22 

Stack Trace: 


[ArgumentOutOfRangeException: 'itemImage1' has a SelectedValue which is 
invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.set_SelectedValue(String value) 
+9703262
Pages_CreateBrands.Page_Load(Object sender, EventArgs e) in 
c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\Pages\CreateBrands.aspx.cs:22
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, 
EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
Boolean includeStagesAfterAsyncPoint) +678
Posted
Updated 4-Aug-17 1:00am

You need to change your Page_Load code so that it only initializes the images when you first load the page. At the moment it's doing it every time you load or postback the page.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string itemselectedValue1 = itemImage1.SelectedValue;
        ShowItemImages1();
        itemImage1.SelectedValue = itemselectedValue1;
 
        string itemselectedValue2 = itemImage2.SelectedValue;
        ShowItemImages2();
        itemImage2.SelectedValue = itemselectedValue2;
    } 
}

One other piece of advice - you're learning about ASP.NET WebForms which is quite old technology. Unless you have a specific reason for learning this (i.e. maintaining a legacy project) you would be much better off investing your time in learning ASP.NET MVC, ASP.NET Core, Web API and/or AngularJS.
 
Share this answer
 
Comments
BebeSaiyan 3-Aug-17 10:05am    
I've tried your solution, the radiobutton is now working from guitar to bass. But the problem is that the ShowItemImages1 and ShowItemImages2 did not bind the images to the dropdownlist. The dropdownlist is empty. Any further ideas? By the way, thanks for the advise.
[no name] 4-Aug-17 6:59am    
I missed your radio buttons automatically posting back. Take a look at solution 2 below..
Updated to take the autopostback on the radio buttons into account and also to reduce the amount of repeated code..
protected void Page_Load(object sender, EventArgs e)
{
    ShowItemImages();
}

private void ShowItemImages()
{
    var imageList = new ArrayList();

    if (itemType1.Checked)
    {
        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();

    }
    else if (itemType2.Checked)
    {

        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();
}
 
Share this answer
 
v2
Comments
BebeSaiyan 7-Aug-17 11:04am    
Sorry about the previous comment. It turns out that it is working. Thanks for the help sir.
[no name] 8-Aug-17 4:50am    
No problem, glad it helped you out.
BebeSaiyan 9-Aug-17 11:06am    
Hi again! I noticed something strange in this solution just today. Lets say for itemImage1, i have images 1,2 and 3 and I chose to select image 3, it will always return image 1. No matter what image i choose, it will always return image 1. Also for itemImage2, whatever the image that was chosen for itemImage1, it will replicate it, which is not the output i envision. Any solutions for this? Sorry for this. I just noticed the bug today.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900