Click here to Skip to main content
15,888,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
private void SaveCheckedValues()
        {
            System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in grdRpt.Rows)
            {
                object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
                index = (int)0;
                bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

                // Check in the Session
                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;
        }


when i run the above 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 show in below line as follows

object o = grdRpt.DataKeys[gvrow.RowIndex].Value;


In the gridview values as follows

transacteeid      totalprice   Qty   Isactive

109453628727        25           1      1
109453628727        15           2      1
565676778            8           3      1


When i debug and check in the value i get value as "109453628727"

how to solve this error. what is the mistake in my above code.

What I have tried:

My code as follows

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
index = (int)0;
bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

// Check in the Session
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;
}

when i run the above 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 show in below line as follows

object o = grdRpt.DataKeys[gvrow.RowIndex].Value;


In the gridview values as follows

transacteeid totalprice Qty Isactive

109453628727 25 1 1
109453628727 15 2 1
565676778 8 3 1

When i debug and check in the value i get value as "109453628727"

how to solve this error. what is the mistake in my above code.
Posted
Updated 18-Jun-18 9:53am
v2

1 solution

you are trying to access an index which is not in the array. This will happen normally if you try to access 5th member in an array with 3 elements.

try to check value of gvrow.RowIndex and if the array grdRpt.DataKeys have that much element.
 
Share this answer
 
Comments
nar86 18-Jun-18 6:14am    
ok.

in my below code what changes i have to made.

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
index = (int)0;
bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

// Check in the Session
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;
}

when i run the above 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 show in below line as follows

object o = grdRpt.DataKeys[gvrow.RowIndex].Value;

in my above code what changes i have to made.
nar86 19-Jun-18 1:46am    
please let me know how to solve this issue
Jinto Jacob 19-Jun-18 1:57am    
you have to do a line by line debug to check what are the values for the index values.

also check if this works

grdRpt.DataKeys[gvrow.Row.RowIndex].Value

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