ASP.Net error: DropDownList has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

on Jul 04, 2014 09:20 AM

I got this error when select a row from a grid view to update

'ddl_ctegOf_Product_update' 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: 'ddl_ctegOf_Product_update' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Source Error: 

Line 265:            txt_prodID_update.Text = gridTo_updateFrom.SelectedRow.Cells[1].Text;
Line 266:            txt_prod_name_update.Text = gridTo_updateFrom.SelectedRow.Cells[2].Text;
Line 267:            ddl_ctegOf_Product_update.SelectedValue = gridTo_updateFrom.SelectedRow.Cells[3].Text;
Line 268:            txt_cateIn_hardCopy_update.Text = gridTo_updateFrom.SelectedRow.Cells[4].Text.Replace(" ", "");
Line 269:            txt_prodID_update.ReadOnly = true;



The code of update:


private void PopulateDropDownList()
            string constr = ConfigurationManager.ConnectionStrings[1].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
                using (SqlCommand cmd = new SqlCommand("SELECT CategoryProd_ID,Cate_Pro_Name FROM CategoryOfProduct", con))
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        DataSet ds = new DataSet();
this.ddl_ctegOf_Product_update.DataTextField = "Cate_Pro_Name";
                        this.ddl_ctegOf_Product_update.DataValueField = "CategoryProd_ID";
                        this.ddl_ctegOf_Product_update.DataSource = ds; this.ddl_ctegOf_Product_update.DataBind();
                        this.ddl_ctegOf_Product_update.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select--", "0"));
 protected void gridTo_updateFrom_SelectedIndexChanged(object sender, EventArgs e)
            txt_prodID_update.Enabled = true;
            txt_prod_name_update.Enabled = true;
            ddl_ctegOf_Product_update.Enabled = true;
            txt_cateIn_hardCopy_update.Enabled = true;

            txt_prodID_update.Text = gridTo_updateFrom.SelectedRow.Cells[1].Text;
            txt_prod_name_update.Text = gridTo_updateFrom.SelectedRow.Cells[2].Text;
            ddl_ctegOf_Product_update.SelectedValue = gridTo_updateFrom.SelectedRow.Cells[3].Text;
            txt_cateIn_hardCopy_update.Text = gridTo_updateFrom.SelectedRow.Cells[4].Text.Replace(" ", "");
            txt_prodID_update.ReadOnly = true;




<asp:GridView ID="gridTo_updateFrom" runat="server" BackColor="White" 
                                        BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" 
                                        CellPadding="4" onselectedindexchanged="gridTo_updateFrom_SelectedIndexChanged"  
                                        AutoGenerateColumns = "false" ShowHeader = "false">
                                            <asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Select" 
                                                ShowHeader="True" Text="&gt;&gt;&gt;" />
                                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                                        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Center" />
                                        <RowStyle BackColor="White" ForeColor="#003399" />
                                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                        <SortedAscendingCellStyle BackColor="#EDF6F6" />
                                        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                                        <SortedDescendingCellStyle BackColor="#D6DFDF" />
                                        <SortedDescendingHeaderStyle BackColor="#002876" />
                                        <asp:BoundField ItemStyle-Width = "150px" DataField = "Pro_ID"  />
                                        <asp:BoundField ItemStyle-Width = "150px" DataField = "Pro_Name" />
                                        <asp:BoundField ItemStyle-Width = "150px" DataField = "Cate_Pro_Name" />
                                        <asp:BoundField ItemStyle-Width = "150px" DataField = "Category_In_Hard_Copy" />



Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jul 07, 2014 05:40 AM

You need to use FindByValue or FindByText. Consider this example


<asp:DropDownList ID="ddlBranches" runat="server">
    <asp:ListItem Text="Please Select" Value="0" />
    <asp:ListItem Text="IT" Value="1" />
    <asp:ListItem Text="CS" Value="2" />
    <asp:ListItem Text="CV" Value="3" />
    <asp:ListItem Text="ME" Value="4" />
    <asp:ListItem Text="EE" Value="5" />


protected void Page_Load(object sender, EventArgs e)
    //It will work as ME is there in the DropDownList Item
    this.ddlBranches.Items.FindByText("ME").Selected = false;
    // It will also work as 4 is present in ListItem values
    this.ddlBranches.Items.FindByValue("4").Selected = false;
    // This will not work as 8 is not in the List of DropDownList Item values. So use If condition
    if (this.ddlBranches.Items.FindByValue("8") != null)
        this.ddlBranches.Items.FindByValue("8").Selected = true;