I am getting a "has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value" error when clicking the edit button on a gridview. i can click "insert" and insert a new record without issue, but when i try to edit the same record, the error appears. This doesn't make sense to me since the datasource and controls in both the insert statement and the edit statement are identical.
Source code:
<asp:TemplateField HeaderText="TT Status" SortExpression="TT Status">
<EditItemTemplate>
<asp:RadioButtonList ID="rdottstatus" runat="server" Text='<%# Bind("TTStatus") %>' Width="100" RepeatDirection="Horizontal">
<asp:ListItem Value="0" Text ="Received"></asp:ListItem>
<asp:ListItem Value="1" Text ="Not Received"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TTStatus") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="ftrrdottstatus" runat="server" Width="100" RepeatDirection="Horizontal">
<asp:ListItem Value="0" Text ="Received"></asp:ListItem>
<asp:ListItem Value="1" Text ="Not Received"></asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Confirmation Status" SortExpression="Confirmation Status">
<EditItemTemplate>
<asp:RadioButtonList ID="rdoConfirmation" runat="server" Text='<%# Bind("confirmationstatus") %>' Width="100" RepeatDirection="Horizontal">
<asp:ListItem Value="0" Text ="Confirmed"></asp:ListItem>
<asp:ListItem Value="1" Text ="Not Confirmed"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("confirmationstatus") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="ftrrdoConfirmation" runat="server" Width="100" RepeatDirection="Horizontal">
<asp:ListItem Value="0" Text ="Confirmed"></asp:ListItem>
<asp:ListItem Value="1" Text ="Not Confirmed"></asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
Code behind:
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtam = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtamtt");
TextBox txtper = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtper");
TextBox txtconf = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtconfirm");
TextBox txtdiff = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtdifference");
RadioButtonList rdott = (RadioButtonList)gvDetails.Rows[e.RowIndex].FindControl("rdottstatus");
RadioButtonList rdoconfirm = (RadioButtonList)gvDetails.Rows[e.RowIndex].FindControl("rdoConfirmation");
Int64 currInvoiceId = Convert.ToInt64(Request.QueryString["edit_invoice_id"]);
try
{
MySqlConnection connect;
string connectStr = ConfigurationManager.ConnectionStrings["ExpoCrmConnectionString"].ToString();
connect = new MySqlConnection(connectStr);
// string queryStr = "insert into payment_receipt_status(invoic_id,Amount,Percentage,TTStatus,confirmationstatus,confirmationamount,Differenceamount)values(?iid,?Amount,?Percentage,?TTStatus,?confirmationstatus,?confirmationamount,?Differenceamount)";
string queryStr = "update payment_receipt_status set Amount=?Amount,Percentage=?Percentage,TTStatus=?TTStatus,confirmationstatus=?confirmationstatus,confirmationamount=?confirmationamount,Differenceamount=?Differenceamount where invoic_id=?iid";
connect.Open();
MySqlCommand command = new MySqlCommand(queryStr, connect);
command.Parameters.AddWithValue("?iid", currInvoiceId);
command.Parameters.AddWithValue("?Amount", txtam.Text);
command.Parameters.AddWithValue("?Percentage", txtper.Text);
command.Parameters.AddWithValue("?TTStatus", rdott.SelectedItem.Text);
command.Parameters.AddWithValue("?confirmationstatus", rdoconfirm.SelectedItem.Text);
command.Parameters.AddWithValue("?confirmationamount", txtconf.Text);
command.Parameters.AddWithValue("?Differenceamount", txtdiff.Text);
command.ExecuteNonQuery();
connect.Close();
FillVendorGrid();
txtam.Text = "";
txtper.Text = "";
txtconf.Text = "";
txtdiff.Text = "";
connect.Close();
}
catch (Exception ex)
{
}
}