user3247426
user3247426

Reputation: 127

Index out of range exception while deleting row from GridView

I am getting error like

"Index was out of range. Must be non-negative and less than the size of the collection"

when trying to delete row of gridview. My aspx.cs code is below:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        main delete=new main();

        // int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells["id"].Value);
        // string ID = GridView1.Rows[e.RowIndex].ToString();
        sQLcONN.Open();
        string Query = "delete  * from shoppingcart where shoppingcart.with_puja = '"+Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())+"' ";
        delete.deleteData(Query);
        Bindgrid();
        sQLcONN.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

Upvotes: 1

Views: 3930

Answers (6)

DevBodin
DevBodin

Reputation: 211

Alright, this question annoyed the Bejebus out of me, but couldn't figure it out until today.

My Situation:

  • A database novice.

  • Set up my own database.

  • Using ASP.NET with MySQL, phpMyAdmin

  • For some reason, only one of my table's had this issue.


In searching for a solution, I decided to open up my database and change the offending table's storage engine from MyISAM to InnoDB.

And that alone fixed the issue.

But that's only a clue as to the real problem. In my case, the offending table was far more read-heavy, changing only occasionally, so MyISAM is what I wanted it to be.

The key difference here is that MyISAM is stricter with input. As you can see above, there's a lot of Int32 converting going on. So, try this:

  • Open up your database, table
  • Check your table's id field
  • Is its type some form of "Int"
  • Is the Value/Length field anything other than 32?

Hope that saves someone else some time and grief.

Upvotes: 1

Adeel Rauf
Adeel Rauf

Reputation: 1

sum += Convert.ToInt32(GridView1.Rows[i].Cells[3].Text);

Upvotes: 0

Mihai Hantea
Mihai Hantea

Reputation: 1743

You can pass the row index as CommandArgument.

In the event method on server:

int rowIndex = int.Parse(e.CommandArgument.ToString());
string val = (string)this.grid.DataKeys[rowIndex]["myKey"];

On the button use:

CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'

EDIT:

Query:

string Query = "delete  * from shoppingcart where shoppingcart.with_puja = '"+val +"' ";

I recommend to use parameters to avoid sql injections.

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

Try this

string Query = "delete * from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString()) + "'";

Upvotes: 0

Jon P
Jon P

Reputation: 821

Maybe the DataKeys part is the culprit, it must be something like this:

string Query = "delete from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(e.Item.DataKeys["nameOfKey"].Value).ToString() + "'";

or something like this:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        main delete = new main();
        DataTable dt = GridView1.DataSource as DataTatable;

        sQLcONN.Open();
        string Query = "delete from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(dt.Rows[e.RowIndex]["nameOfKey"]).ToString() + "'";
        delete.deleteData(Query);
        Bindgrid();
        sQLcONN.Close();

    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

Where nameOfKey is the column name of the table which is with_puja in your case and GridView1 must be e.Item if e has an Item property.

Upvotes: 1

Afnan Ahmad
Afnan Ahmad

Reputation: 2542

Your DataKeys in GridView would be like:

 DataKeyNames="Id, foo, foo"

and then try to use string like this:

Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString()); // 0 is the index and in this case Id will be at zero

Upvotes: 1

Related Questions