Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey all

when i try to select command the following error comes in my code

ERROR

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

CODE

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DepartmentSupervisorInfo.DeleteDeptSupervisor(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
                GridView1.DataSource = DepartmentSupervisorInfo.getDeptSupervisor();
                GridView1.DataBind();
            }
            else if (e.CommandName == "Select")
            {
                DataSet ds = DepartmentSupervisorInfo.getDeptSupervisorbyID(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
                Hdnfld_deptsupid.Value = ds.Tables[0].Rows[0]["DepartmentSupervisorID"].ToString();
                Dd_supDepid.DataValueField = ds.Tables[0].Rows[0]["DepartmentID"].ToString();
                dd_supEmpid.DataValueField = ds.Tables[0].Rows[0]["EmployeeID"].ToString();
           }
        }

ERROR IN THIS LINE

C#
DataSet ds = DepartmentSupervisorInfo.getDeptSupervisorbyID(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
Posted

Problem is at this line

C#
GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text


Check that GridView1.Rows contains that many rows. It seems that it does not. For example Convert.ToInt32(e.CommandArgument) is returning 2 but the GridView1 does not contain two rows.

Check the value of Convert.ToInt32(e.CommandArgument) and fix it
 
Share this answer
 
C#
MessageBox.Show(e.CommandArgument);


C#
//row number is:e.CommandArgument+1
//cell number is:3
MessageBox.Show(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text);


debug the previous code and paste the result to me .
 
Share this answer
 
v2
Comments
Diya Ayesa 26-Jul-13 2:23am    
where i paste this code?i dont show msessage box
Chui PuiKwan 26-Jul-13 2:31am    
paste the content of the messagebox here .
or
ur...what is your communication tools ?
or
send image message to my email
Diya Ayesa 26-Jul-13 2:34am    
ok whats ur email id?
Chui PuiKwan 26-Jul-13 2:48am    
puikwanchui@gmail.com
Diya Ayesa 26-Jul-13 2:50am    
mail sent with images

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