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();
da.Fill(ds);
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;
}
HTML:
<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">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Select"
ShowHeader="True" Text=">>>" />
</Columns>
<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" />
<Columns>
<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" />
</Columns>
</asp:GridView>