-1

gridview have a problem

    protected void DeleteRowButton_Click(Object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionAkaryakit"].ConnectionString);
        con.Open();

        int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        string str1 = "DELETE FROM dbo.Urun_Satis where Musteri_Bilgisi=" + i + "";
        SqlCommand cmd = new SqlCommand(str1, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

The directory was out of range. It should not be a negative value and should be smaller than the size of the collection. \ R \ nParameter name: index "}

MMM
  • 3,132
  • 3
  • 20
  • 32
  • What is your issue? – MMM Jun 09 '19 at 18:02
  • The directory was out of range. It should not be a negative value and should be smaller than the size of the collection. \ R \ nParameter name: index "} for int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); – berat güdek Jun 09 '19 at 18:07
  • 2
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ňɏssa Pøngjǣrdenlarp Jun 09 '19 at 18:17

1 Answers1

0

The issue is with this line:

int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

What you need to check ?

  • Does your Grid View has DataKeys defined ? Because you are accessing the data keys collection of the grid view.
  • Assuming Datakeys are defined properly, do you really have the correct no of keys defined ?

When I say, correct no of keys, what if e.RowIndex = 20 ? this means: GridView1.DataKeys[20].Value ? Do you have this number of keys defined for Grid View ??

R.C
  • 10,417
  • 2
  • 35
  • 48