1
Reply

delete checked record from the data base and gridview

jignesh kadvani

jignesh kadvani

Oct 8 2008 7:35 AM
2.6k
hi..
i am using gridview. and i have a  checkbox  as a template column in gridview . when i checked single record and press delete button,  then record is successfully perform delete. but when i check multiple record and click the delete then it gives  following error:
Index was out of range. Must be non-negative and less than the size of the collection.<br>Parameter name: index


Server Error in '/' Application.

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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:

Line 101:                if (chkBox.Checked)
Line 102:                {
Line 103:                    int id = Convert.ToInt32(gvEmployee.DataKeys[objData.RowIndex].Value.ToString());
Line 104:
Line 105:                    objB.Delete(id);

i am using below code to perform delete operation i am developing application in three-tire architecture.

protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow objData in gvEmployee.Rows)
            {
                CheckBox chkBox = (CheckBox)objData.FindControl("chkDelete");
                if (chkBox.Checked)
                {
                    int id = Convert.ToInt32(gvEmployee.DataKeys[objData.RowIndex].Value.ToString());

                    objB.Delete(id);
                    fillEmployeeDetail();

                }

            }

        }

can anyone give me solution. with code in asp.net,C#

Answers (1)