Hi all,
I have the following code in c#, where I am trying to show the select multiple items stored in database in the aspx page from c#.
But I have this error.
'ListBox1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'ListBox1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error:
Line 1345: foreach (ListItem item in ListBox1.Items)
Line 1346: {
Line 1347: ListBox1.SelectedValue += sdr["Item_30"].ToString() + "; ";
Line 1348: }
Line 1349:
What to do and how do I resolve this?
Multiple items stored in database
A; C;
Code-behind
if (!String.IsNullOrEmpty(sdr["Item_30"].ToString()))
{
foreach (ListItem item in ListBox1.Items)
{
ListBox1.SelectedValue += sdr["Item_30"].ToString() + "; ";
}
}
ASPX Code:
<asp:ListBox ID="ddlItem_30" runat="server" SelectionMode="Multiple" Height="80"
CssClass="text-search">
<asp:ListItem Text="[ === Select === ]" Value=""></asp:ListItem>
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:ListBox>