Ashish
Ashish

Reputation: 107

Dropdown has a selected value which is invalid

I have been working on a project and facing an error as i have two textbox and a dropdown when i fill one of the textbox on its selecting_indexchanged the dropdown get filled,what is happening is when i click on reset button to reset the dropdown a textboxes to be blank and dropowns index value to 0,its shows as error as

"dropdown has a SelectedIndex which is invalid because it does not exist in the list of items. Parameter name: value "

<td class="style4">
            <asp:Label ID="lbl_bookname" runat="server" Text="Book Name" 
                ForeColor="Black"></asp:Label>
        </td>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"  TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
        </asp:AutoCompleteExtender>
        <td class="style4">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
                        </asp:ScriptManager>

            <asp:TextBox ID="txt_bookname" runat="server" ValidationGroup="a" OnTextChanged="editiondrpfill_TextChanged" AutoPostBack="true"></asp:TextBox>
             <asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true"  CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
        </asp:AutoCompleteExtender> 
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ErrorMessage="*" ForeColor="Red" ControlToValidate="txt_bookname" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
        </td>
        <td class="style4">
            <asp:Label ID="lbl_condition" runat="server" ForeColor="Black" Text="Condition"></asp:Label>
            </td>
        <td class="style4">
            <asp:TextBox ID="txt_condition" runat="server" BackColor="White" 
                ReadOnly="True" Width="120px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                ControlToValidate="txt_condition" ErrorMessage="*" ForeColor="Red" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style4">
            <asp:Label ID="lbl_edition" runat="server" ForeColor="Black" Text="Edition"></asp:Label>
        </td>
        <td class="style4">
            <asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" 
                Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged" 
                AutoPostBack="true" TabIndex="0">
            </asp:DropDownList>
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
        </td>

CS Code for filling of dropdown

  protected void editiondrpfill_TextChanged(object sender, EventArgs e)
{
   sql = "select bt.booktwo_id,bt.edition,b.bookname from library_book b , library_booktwo bt where bookname ='" + txt_bookname.Text+ "' and bt.book_id=b.book_id";
   ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
   drp_edition.Items.Clear();
    ListItem li = new ListItem();
    li.Text = "Select the value";
     li.Value = "0";
    drp_edition.Items.Add(li);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        li = new ListItem();
         li.Text= ds.Tables[0].Rows[i]["edition"].ToString();
         li.Value = ds.Tables[0].Rows[i]["booktwo_id"].ToString();
        drp_edition.Items.Add(li);
    }
}

cs code for reset button event

  protected void btn_reset_Click(object sender, EventArgs e)
{
    txt_bookname.Text = "";
    txt_condition.Text = "";
    txt_member_id.Text = "";
    txt_nameofstudent.Text = "";
    txt_quantity.Text = "";
    drp_edition.SelectedIndex = 0;
    drp_isbn.SelectedIndex = 0;
}

Upvotes: 0

Views: 829

Answers (2)

MahaSwetha
MahaSwetha

Reputation: 1066

Place the default list item for the dropdown "drp_edition". Since,you are assigned the selectedindex=0 in reset fn. There should some list item in this case.

<td class="style4">
<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" Width="120px"      
onselectedindexchanged="drp_edition_SelectedIndexChanged" AutoPostBack="true" TabIndex="0">
<asp:ListItem Text="--Select--" Value="0" />
</asp:DropDownList>
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"           
ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red" InitialValue="0"    
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>

Upvotes: 0

Rahul
Rahul

Reputation: 5636

Do one thing on your aspx.cs page add a ListItem to dropdown with value 0 like

<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" 
                Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged" 
                AutoPostBack="true" TabIndex="0">
                <asp:ListItem Value="0" Text="Select" ></asp:ListItem>
            </asp:DropDownList>

It will solve your problem.

Hope it works.

Upvotes: 2

Related Questions