Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 below 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

C#
index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;


how to solve this error. please let me know.

What I have tried:

C#
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();
        }
Posted
Updated 14-Jun-18 2:19am
v2
Comments
Richard Deeming 14-Jun-18 10:21am    
REPOST
You have already posted this question:
https://www.codeproject.com/Questions/1248362/How-to-update-selected-checkbox-row-to-database-fr[^]

If you want to update your question with additional information, click the green "Improve question" link and edit your question. DO NOT post your update as a new question.
Richard Deeming 14-Jun-18 10:23am    
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where Id=" + key.ToString(), con);


Don't do it like that!

Whilst you're safe in this specific scenario, writing code like that can and will lead to SQL Injection vulnerabilities in your code. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where Id = @Id", con))
{
    cmd.Parameters.AddWithValue("@Id", key);
    cmd.ExecuteNonQuery();
}

1 solution

The error is exactly what the message says:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
If it occurs on this line:
index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
then gvrow.RowIndex is either negative, or too big for the DataKeys collection. Use the debugger to find out what the actual value is, and what the size of the collection is. Then you can start working out why it's the value it is...

We can't do that for you: we don't have access to your data, and can't run your code under the same conditions you can.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900