Please solve this gridBind ERROR ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itsvineeth209
    New Member
    • Mar 2008
    • 4

    Please solve this gridBind ERROR ASP.NET

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Argument OutOfRangeExcep tion: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Source Error:


    Line 244: {
    Line 245: // Retreive the Employee ID
    Line 246: int RollNo = Convert.ToInt32 (GridView1.Data Keys[row.RowIndex].Value);
    Line 247: // Pass the value of the selected RollNo to the Delete //Method.
    Line 248: DeleteStudent(R ollNo);



    This is my actual code

    protected void Button2_Click(o bject sender, EventArgs e)
    {
    // Looping through all the rows in the GridView
    foreach (GridViewRow row in GridView1.Rows)
    {
    CheckBox checkbox1 = (CheckBox)row.F indControl("Che ckBox1");

    //Check if the checkbox is checked.
    //value in the HtmlInputCheckB ox's Value property is set as the //value of the delete method's parameter.
    if (checkbox1.Chec ked)
    {
    // Retreive the Employee ID
    int RollNo = Convert.ToInt32 (GridView1.Data Keys[row.RowIndex].Value);
    // Pass the value of the selected RollNo to the Delete //Method.
    DeleteStudent(R ollNo);
    BindGrid();


    }
    }
    }
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi, have you solved this yet? It looks like you might be passing an invalid RowIndex in to GridView1.DataK eys[].....can you step it through on debug and see what happens? Does it error the first time it hits this line or does it [font=Verdana][size=2]loop through a few times first - if so what are the values when it errors?[/size][/font]

    Dr B

    Comment

    Working...