locked
problem in updating gridview (C#) RRS feed

  • Question

  • User-1792319420 posted
    hi friends! i am getting big trouble in updating gridview.i am doing it for the first time and don't know how to resolve the error.pls help me out! my aspx code for gridview is: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" onselectedindexchanged="GridView1_SelectedIndexChanged1"> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <Columns> <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="personC" HeaderText="personC" SortExpression="personC" /> <asp:BoundField DataField="occasion" HeaderText="occasion" SortExpression="occasion" /> <asp:BoundField DataField="dateR" HeaderText="date" SortExpression="dateR" /> </Columns> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <EmptyDataTemplate> id </EmptyDataTemplate> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> </asp:GridView> my coding goes something like this: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string id1 = GridView1.Rows[e.RowIndex].Cells[0].Text; string personC = (GridView1.Rows[e.RowIndex].Cells[1].Controls[1] as TextBox).Text; string occasion = (GridView1.Rows[e.RowIndex].Cells[2].Controls[2] as TextBox).Text; string dateR = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text; SqlCommand com = new SqlCommand("update tblSetReminder set personC='" + personC.Text + "',occasion='" + occasion.Text + "',dateR='" + DateR.Text + "' where id=id1", con1); con1.Open(); com1.ExecuteNonQuery(); con1.Close(); GridView1.EditIndex = -1; DataBind(); com1.Connection = con1; com1.CommandText = "select id,personC,occasion,dateR from tblSetReminder where userID= @userId"; com1.Parameters.AddWithValue("@userId", Session["login"].ToString()); adp1.SelectCommand = com1; adp1.Fill(ds1); GridView1.DataSource = ds1.Tables[0]; DataBind(); } now i am getting error "Specified argument was out of the range of valid values.Parameter name: index" on the following line of code string personC = (GridView1.Rows[e.RowIndex].Cells[1].Controls[1] as TextBox).Text; pls help me out! thanks!
    Wednesday, April 29, 2009 1:31 AM

Answers

  • User-1814013008 posted

    debug step by step where it is coming out of scope.

    If you would like to use any control in Update mode, create template columns and search using "FindControl" method instead of Control Index in row like below.

    <STRIKE>GridView1.Rows[e.RowIndex].Cells[0].Text</STRIKE>

    ((TextBox)GridView1.Rows[e.RowIndex].FindControl("stringid1")).Text

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 29, 2009 2:58 AM

All replies

  • User-1814013008 posted

    debug step by step where it is coming out of scope.

    If you would like to use any control in Update mode, create template columns and search using "FindControl" method instead of Control Index in row like below.

    <STRIKE>GridView1.Rows[e.RowIndex].Cells[0].Text</STRIKE>

    ((TextBox)GridView1.Rows[e.RowIndex].FindControl("stringid1")).Text

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 29, 2009 2:58 AM
  • User-1792319420 posted
    hi! my gridview is like this: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" onselectedindexchanged="GridView1_SelectedIndexChanged1"> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <Columns> <asp:TemplateField HeaderText="id" SortExpression="id"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="personC" SortExpression="personC"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("personC") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("personC") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="occasion" SortExpression="occasion"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("occasion") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("occasion") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="date" SortExpression="dateR"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("dateR") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("dateR") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <%--<EmptyDataTemplate> id </EmptyDataTemplate>--%> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> </asp:GridView> and i have changed the coding : string id1 = GridView1.Rows[e.RowIndex].FindControl("id1").ToString(); but it again giving error on "id1": Object reference not set to an instance of an object i want to know from where this control come in findcontrol???
    Wednesday, April 29, 2009 3:31 AM