problem in editing in gridview
-
hi i have code at grid view editing have proble at update time protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //fetch datakey/primarykey for identifyning the row string materialrecipt_no = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value); TextBox materialrecip_date = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]; TextBox pqty = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[1]); TextBox enterby = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]); TextBox ptype = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]); TextBox remark = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[1]); SqlCommand com = new SqlCommand("update material_incoming set materialrecip_date='" + materialrecip_date.Text + "',pqty='" + pqty.Text + "',enterby='" + enterby.Text + "',ptype='" + ptype.Text + "',remark='" + remark.Text + "' where materialrecipt_no=" + materialrecipt_no + "", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; //to go back to the previous position // fetch and rebind the data. BindGrid(); } but it gives an error error is Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
-
hi i have code at grid view editing have proble at update time protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //fetch datakey/primarykey for identifyning the row string materialrecipt_no = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value); TextBox materialrecip_date = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]; TextBox pqty = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[1]); TextBox enterby = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]); TextBox ptype = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]); TextBox remark = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[1]); SqlCommand com = new SqlCommand("update material_incoming set materialrecip_date='" + materialrecip_date.Text + "',pqty='" + pqty.Text + "',enterby='" + enterby.Text + "',ptype='" + ptype.Text + "',remark='" + remark.Text + "' where materialrecipt_no=" + materialrecipt_no + "", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; //to go back to the previous position // fetch and rebind the data. BindGrid(); } but it gives an error error is Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Set a break point in your code, Find which line of code throws the error check the following e.RowIndex - if it is a negative value, it may throw an error at GridView1.Rows[e.RowIndex] Cells collection - check whether indexs of the grid cells are within the range.
-
hi i have code at grid view editing have proble at update time protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //fetch datakey/primarykey for identifyning the row string materialrecipt_no = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value); TextBox materialrecip_date = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]; TextBox pqty = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[1]); TextBox enterby = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]); TextBox ptype = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]); TextBox remark = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[1]); SqlCommand com = new SqlCommand("update material_incoming set materialrecip_date='" + materialrecip_date.Text + "',pqty='" + pqty.Text + "',enterby='" + enterby.Text + "',ptype='" + ptype.Text + "',remark='" + remark.Text + "' where materialrecipt_no=" + materialrecipt_no + "", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; //to go back to the previous position // fetch and rebind the data. BindGrid(); } but it gives an error error is Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Hi, What i think the problem is in the place where you refer to your control using Cells[index].Controls[1] and there would have been only one control and that too at position [0] not [1]. So try using the code like this as you do know the name of the control which you want to fetch using the code for updation purpose. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //fetch datakey/primarykey for identifyning the row string materialrecipt_no = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value); TextBox materialrecip_date = (TextBox)GridView1.Rows[e.RowIndex].FindControl("ControlName"); TextBox pqty = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ControlName")); TextBox enterby = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ControlName")); TextBox ptype = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ControlName")); TextBox remark = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ControlName")); SqlCommand com = new SqlCommand("update material_incoming set materialrecip_date='" + materialrecip_date.Text + "',pqty='" + pqty.Text + "',enterby='" + enterby.Text + "',ptype='" + ptype.Text + "',remark='" + remark.Text + "' where materialrecipt_no=" + materialrecipt_no + "", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; //to go back to the previous position // fetch and rebind the data. BindGrid(); } Please mark as answer if problem gets resolved. Regards, Kaushal Arora