Click here to Skip to main content
11,705,208 members (50,111 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
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);"


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 14-Mar-12 1:28am
Edited 14-Mar-12 1:31am
digimanus39.1K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
        }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 OriginalGriff 244
2 ppolymorphe 126
3 F-ES Sitecore 110
4 Andy Lanng 85
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100