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