Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir,
i have following error when i am accessing the stateid in grid the error is

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

error occured at line
"to.StateId1 = Convert.ToInt32(GdState.DataKeys[row.RowIndex].Value);"


C#
protected void btnDeleteState_Click(object sender, EventArgs e)
   {
       ClassMethods cm = new ClassMethods();
       ToContainer to = new ToContainer();
       int counter = 0;
       foreach (GridViewRow row in GdState.Rows)
       {
           CheckBox Chkbxstate = row.FindControl("chkchild") as CheckBox;
           if (Chkbxstate.Checked)
           {

               to.StateId1 = Convert.ToInt32(GdState.DataKeys[row.RowIndex].Value);//error at this line

               counter += cm.DeleteState(to);
           }
       }
       if (counter > 0)
       {
           lbldel.ForeColor = System.Drawing.Color.Blue;
           lbldel.Text = counter.ToString() + " States Deleted Successfully";
       }

       ShowGridState();

       //ClassMethods cm = new ClassMethods();

   }
please help me
thanx & regards
dnyaneshwar kawathe
Posted
Updated 14-Mar-12 1:31am
v2

1 solution

C#
foreach (GridViewRow row in GdState.Rows)
        {
            GdState.SelectedIndex = row.RowIndex;
            CheckBox Chkbxstate = row.FindControl("chkchild") as CheckBox;
            if (Chkbxstate != null && Chkbxstate.Checked)
            {

                to.StateId1 = Convert.ToInt32(row.SelectedValue);//error at this line

                counter += cm.DeleteState(to);
            }
GdState.SelectedIndex = -1;
        }
 
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