0

I am trying to update gridview values, but I am getting error saying:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

I want to fetch values of row I selected for edit to update that values in my database and to display edited vales in gridView.

Below is my code.

    .aspx file
    <asp:GridView ID="GridView1" DataKeyNames="ID" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"  
            OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <AlternatingRowStyle BackColor="White"/>
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
                <asp:BoundField DataField="UserID" HeaderText="UserID" DataFormatString="{0:0.##}" />
                <asp:BoundField DataField="UserKey" HeaderText="UserKey" DataFormatString="{0:0.##}" />
                <asp:BoundField DataField="Value" HeaderText="Value" DataFormatString="{0:0.##}" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="updateQuery" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [ID], [UserID], [UserKey], [Value] FROM [FirstTable]"></asp:SqlDataSource>


    .aspx.cs file:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        try
        {
            String UserID = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
            Response.Write(UserID);

            string updateSQL;
            updateSQL = "UPDATE FirstTable SET UserID = @UserID, UserKey = @UserKey, Value = @Value WHERE ID = @ID";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(updateSQL, con);

            cmd.Parameters.AddWithValue("@ID", ID);
            cmd.Parameters.AddWithValue("@UserID", "aaa");
            cmd.Parameters.AddWithValue("@UserKey", "bbb");
            cmd.Parameters.AddWithValue("@Value", "ccc");
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
            }
            finally
            {
                con.Close();
            }
            GridView1.EditIndex = -1;
            // GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
            BindData();
        }
        catch(Exception er)
        {
            Response.Write(er);
        }
    }
1
  • Do you have an update panel in the page? Commented Apr 29, 2017 at 6:22

1 Answer 1

1

The problem is you are accessing wrong columns first 2 cells has edit button and delete button and 3rd cell has ID so your userID is in 4th cell. so you have to access that cell

 String UserID = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.