Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindData();
- }
- }
- protected void BindData()
- {
- String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
- SqlConnection con = new SqlConnection(strConnString);
- SqlCommand cmd = new SqlCommand("select * from [transact].[transaction_item] where status = 'new'", con);
- con.Open();
- SqlDataAdapter da = new SqlDataAdapter();
- DataSet ds = new DataSet();
- da.SelectCommand = cmd;
- da.Fill(ds);
- grdRpt.DataSource = ds;
- grdRpt.DataBind();
- }
- protected void grdRpt_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- grdRpt.PageIndex = e.NewPageIndex;
- BindData();
- }
- private void PopulateCheckedValues()
- {
- System.Collections.ArrayList userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
- if (userdetails != null && userdetails.Count > 0)
- {
- foreach (GridViewRow gvrow in grdRpt.Rows)
- {
- int index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
- if (userdetails.Contains(index))
- {
- CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkselecdata");
- myCheckBox.Checked = true;
- }
- }
- }
- }
- private void SaveCheckedValues()
- {
- System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
- int index = -1;
- foreach (GridViewRow gvrow in grdRpt.Rows)
- {
- index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
- bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;
- if (Session["CHECKED_ITEMS"] != null)
- userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
- if (result)
- {
- if (!userdetails.Contains(index))
- userdetails.Add(index);
- }
- else
- userdetails.Remove(index);
- }
- if (userdetails != null && userdetails.Count > 0)
- Session["CHECKED_ITEMS"] = userdetails;
- }
- protected void btnsubmit_Click(object sender, EventArgs e)
- {
- String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
- SqlConnection con = new SqlConnection(strConnString);
- con.Open();
- foreach (GridViewRow row in grdRpt.Rows)
- {
- int key = (int)grdRpt.DataKeys[row.RowIndex].Value;
- CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
- SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where Id=" + key.ToString(), con);
- cmd.ExecuteNonQuery();
- }
- con.Close();
- }
in the first page five records will be displayed and in the second page another five records will be displayed.
suppose in the first page i check two rows and in the second page i check another two rows means that selected check box rows to be update in the [transact].[transaction_item] in the table.
when i run the code shows error as follows
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
The error shows in below line as follows
index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
how to solve this error. please let me know.