Click here to Skip to main content
15,888,255 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written code for Updating, Deleting, Cancelling & Editing the row in the GridBox. Cancelling & Editing works fine. But when I Click Update or Delete, I get an error stating,
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


What I have tried:

C#
protected void Show_Grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int id = int.Parse(Show_Grid.DataKeys[e.RowIndex].Value.ToString());
    TextBox txt_title = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Title");
    TextBox txt_desc = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Description");
    TextBox drop_prior = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Priority");

    Update_todo(id, txt_title.Text, txt_desc.Text, drop_prior.Text);
    Show_Grid.EditIndex = -1;
    DataBind();
}
Posted
v2
Comments
CHill60 24-May-16 7:54am    
We sort of need to see the code behind the Delete and Update events
Mohamed Sheik M 24-May-16 8:08am    
private void Update_todo(int id, string title, string desc, string prior)
{
string source = "Data Source=.\\SQLEXPRESS;AttachDbFilename=tododb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection dbconnect = new SqlConnection(source);
string query = "UPDATE todolist SET Title='" + title + "', Description='" + desc + "', Priority=" + prior + " WHERE id =" +id+ " ";
SqlCommand cmd = new SqlCommand (query, dbconnect);
dbconnect.Open();
cmd.ExecuteNonQuery();
}

private void Delete_todo(int id)
{
string source = "Data Source=.\\SQLEXPRESS;AttachDbFilename=tododb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection dbconnect = new SqlConnection(source);
string query = "DELETE FROM todolist WHERE id=" + id + "";
SqlCommand cmd = new SqlCommand(query, dbconnect);
dbconnect.Open();
cmd.ExecuteNonQuery();
}
Debug and see which line and which index is coming negative.
Mohamed Sheik M 24-May-16 8:07am    
int id = int.Parse(Show_Grid.DataKeys[e.RowIndex].Value.ToString());
This line gets error
Do you have data keys declared on markup?

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