Forums - C# Corner

Forum guidelines
Narasiman nar

Narasiman nar

  • 1.7k
  • 64
  • 2.5k

Index was out of range. Must be non-negative and less than t

Jun 14 2018 7:10 AM
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. BindData();  
  6. }  
  7. }  
  8. protected void BindData()  
  9. {  
  10. String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;  
  11. SqlConnection con = new SqlConnection(strConnString);  
  12. SqlCommand cmd = new SqlCommand("select * from [transact].[transaction_item] where status = 'new'", con);  
  13. con.Open();  
  14. SqlDataAdapter da = new SqlDataAdapter();  
  15. DataSet ds = new DataSet();  
  16. da.SelectCommand = cmd;  
  17. da.Fill(ds);  
  18. grdRpt.DataSource = ds;  
  19. grdRpt.DataBind();  
  20. }  
  21. protected void grdRpt_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  22. {  
  23. grdRpt.PageIndex = e.NewPageIndex;  
  24. BindData();  
  25. }  
  26. private void PopulateCheckedValues()  
  27. {  
  28. System.Collections.ArrayList userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];  
  29. if (userdetails != null && userdetails.Count > 0)  
  30. {  
  31. foreach (GridViewRow gvrow in grdRpt.Rows)  
  32. {  
  33. int index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;  
  34. if (userdetails.Contains(index))  
  35. {  
  36. CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkselecdata");  
  37. myCheckBox.Checked = true;  
  38. }  
  39. }  
  40. }  
  41. }  
  42. private void SaveCheckedValues()  
  43. {  
  44. System.Collections.ArrayList userdetails = new System.Collections.ArrayList();  
  45. int index = -1;  
  46. foreach (GridViewRow gvrow in grdRpt.Rows)  
  47. {  
  48. index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;  
  49. bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;  
  50. if (Session["CHECKED_ITEMS"] != null)  
  51. userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];  
  52. if (result)  
  53. {  
  54. if (!userdetails.Contains(index))  
  55. userdetails.Add(index);  
  56. }  
  57. else  
  58. userdetails.Remove(index);  
  59. }  
  60. if (userdetails != null && userdetails.Count > 0)  
  61. Session["CHECKED_ITEMS"] = userdetails;  
  62. }  
  63. protected void btnsubmit_Click(object sender, EventArgs e)  
  64. {  
  65. String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;  
  66. SqlConnection con = new SqlConnection(strConnString);  
  67. con.Open();  
  68. foreach (GridViewRow row in grdRpt.Rows)  
  69. {  
  70. int key = (int)grdRpt.DataKeys[row.RowIndex].Value;  
  71. CheckBox cb = (CheckBox)row.FindControl("chkselecdata");  
  72. SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where Id=" + key.ToString(), con);  
  73. cmd.ExecuteNonQuery();  
  74. }  
  75. con.Close();  
  76. }  
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. 

Answers (1)