Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<>"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
i got above error....
code is below:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection cn = new SqlConnection("Data Source=ANCYCHACKO-TOSH;Initial Catalog=tallysoftware;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cn.Open();
    GridViewRow row = GridView1.Rows[e.RowIndex];
    DropDownList dropdownlist1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownlist1");
    TextBox txtEcollected = (TextBox)row.FindControl("txtEcollected");
    TextBox txtEplan = (TextBox)row.FindControl("txtEplan");
    TextBox txtEacoll = (TextBox)row.FindControl("txtEacoll");
    DropDownList dropdownlist2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownlist2");
    TextBox txtEre = (TextBox)row.FindControl("txtEre");
    TextBox txtEdescrptn = (TextBox)row.FindControl("txtEdescrptn");
    int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
    string se_name = dropdownlist1.SelectedItem.Text;
    string collected_date = txtEcollected.Text;
    string amount_plan = txtEplan.Text;
    string amount_collected = txtEacoll.Text;
    string status_ar = dropdownlist2.SelectedItem.Text;
    string remark = txtEre.Text;
    string description = txtEdescrptn.Text;
    cmd.CommandText = "UPDATE tbl_bill SET se_name='" + dropdownlist1.SelectedItem.Text + "' ,collected_date ='" + txtEcollected.Text + "',amount_plan ='" + txtEplan.Text + "',amount_collected ='" + txtEacoll.Text + "',status_ar ='" + dropdownlist2.SelectedItem.Text + "',remark='" + txtEre.Text + "',description='" + txtEdescrptn.Text + "' WHERE id=" + Id + "'";
    cmd.ExecuteNonQuery();

    GridView1.EditIndex = -1;

    Grid();
    cn.Close();
}
Posted
Updated 27-Nov-14 22:45pm
v2
Comments
Tomas Takac 28-Nov-14 4:47am    
What line throws the error? BTW SqlConnection and SqlCommand implement IDisposable.
George Jonsson 28-Nov-14 5:16am    
Check that e.RowIndex has a valid value.
For example:
if (e.RowIndex < 0)
return;

1 solution

You're only using RowIndex so it has to be the culprit. Check that it holds valid value and return if it is -1.

You should wrap everything in try..catch..finally and in finally block dispose of your disposable objects (SQLConnection and SQLCommand)
 
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