0

I want to delete image from datalist on the behalf of id but this code is not working Error:Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

public void show_top_one()
{
    BusinessLogic b1 = new BusinessLogic();
    string query = "select txt_img_name from tbl_gallery2";
    DataSet ds = b1.GetDataSet(query);
    DataList1.DataSource = ds.Tables[0];
    DataList1.DataBind();
}

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
    BusinessLogic b1 = new BusinessLogic();
    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
    string query = "Delete from tbl_gallery2 where id='"+id+"'";
    b1.ExecuteQuery(query);
    show_top_one();

}

}

1
  • check this value DataList1.DataKeys[e.Item.ItemIndex] weather it is null or not. Oct 25, 2013 at 9:39

5 Answers 5

2

Please change your select query.

string query = "select id,txt_img_name from tbl_gallery2";

and now please add DataKeyName to your datalist like

<asp:DataList DataKeyField="id"

So this will add Id column to datakey. Then you can get value of it in backend.

1
  • I was writing half way and saw your post. OP select query suggests that a DataKeyField property is not assigned. +1
    – Kaf
    Oct 25, 2013 at 9:37
0

Try This : You missed .Value, i guess.

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
    BusinessLogic b1 = new BusinessLogic();
    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].Value);
    string query = "Delete from tbl_gallery2 where id='"+id+"'";
    b1.ExecuteQuery(query);
    show_top_one();

}
1
  • strange first converting to Int32 then passing it in single quotes in the parameter?? Oct 25, 2013 at 9:34
0

don't need single quotations, if it is int

 string query = "Delete from tbl_gallery2 where id="+id;

and also you need to select id column in your select query as well as data key

0

Ok your code is not giving me a lot of info to work on but if you have you DataSet configured right then you could write a method to: 1. get the image by id. 2. delete the selected image.

 public "classname where the image is" GetById(int id)
    {
        return ds.Find(id);  //ds is your DataSet
    }

and after finding it

 public void Delete(int id)
    {
        var entity = this.GetById(id);
        if (entity!=null)
        {
            this.Delete(entity);
        }
    }

This should be a clean and nice answer :)

Good luck!

0

First of all you need to check this value

 DataList1.DataKeys[e.Item.ItemIndex].Value

You might be getting null value in the above parameter. If yes you may not have set the data-key-filed in your data-list.

So you will need to set the DataKeyField in your DataList first and then use the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.