Por Weiting
Por Weiting

Reputation: 167

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

i am having a grid view and when i click page 2 the error given was highlighted at my sql command which is this : SqlCommand cmdShow = new SqlCommand("delete from Student where NRIC= '" + GridView1.DataKeys[0].Value + "'", conn); and state that the error was : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

thsi is my code :

  protected void GridView1_RowDeleted(Object sender, GridViewCommandEventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=MCDU\\SQLEXPRESS;Initial Catalog=n;Integrated Security=True");

        conn.Open();

        SqlCommand cmdShow = new SqlCommand("delete from Student where NRIC= '" + GridView1.DataKeys[0].Value + "'", conn);

        cmdShow.ExecuteNonQuery();

        conn.Close();

        Response.Redirect("studentParticulars.aspx");
    }

Upvotes: 1

Views: 10029

Answers (4)

R.C
R.C

Reputation: 10565

So, As you confirmed that when you click on delete button, row should be deleted. From what you have in your GridView Markup, Please make below corrections:

Since you are using SqlDataSource as the DataSourceID for your GridView, This SqldataSource control will take care of delete functionality. This is because when we click delete button the GridView will accordingly pass on this to SqlDataSource with correct parameters. Simply set the Delete command of this DataSource control as:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindEntities %>"  
SelectCommand="Select top 20 ContactName,CompanyName,CustomerID from Customers" 
DeleteCommand="Delete from Customers where CustomerID=@CustomerID" 
UpdateCommand="UPDATE Customers SET ContactName = ContactName,
CompanyName=@CompanyName WHERE CustomerID = @CustomerID" >
</asp:SqlDataSource>

Now, you also need to correct your GridView markup, since you require that Delete button will delete a row and you have assigned the GridView1_RowDeleted function as a event handler to onrowcommand which really doesn't makes any sense. Your GridView markup should be like : [ No onrowcommand. Keep the AutoGenerateDeleteButton="true" ]

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" Height="50px"
Width="487px" AutoGenerateDeleteButton="True" BorderStyle="None" PageSize="15">

Now, just make sure the row you want to delete doesn't has any child rows set with Foreign Key. In case you have , then delete the child rows first.

Also, when you click on any paging links ( here you were clicking page 2), then also onrowcommand is called and then you were deleting a row which doesn't makes any logic.

That's it. Once you click delete button in your Grid view, it will delete the row, provided you have set up the Delete command correctly for SqlDataSource control.

Just reiterating the mistakes in your code:

  • With DataSourceId as SqlDataSource control, you need to set the Delete command of this datasource control.when we click delete button the GridView will accordingly pass on this to SqlDataSource with correct parameters

  • onrowcommand should not be used to delete a row.

  • In case you want some code to be executed after a row is deleted, use OnRowDeleted event

Let me know if you need more clarifications.

Upvotes: 1

Low Chee Mun
Low Chee Mun

Reputation: 610

check this out, always remember u need to put this as your gridview pagination

 protected void grdView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
    grdView.PageIndex = e.NewPageIndex;
    grdView.DataSource = bindgrid();
    grdView.DataBind();  
}

and this is the sample code link

http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

hope this can help u out=)

Upvotes: 0

shf301
shf301

Reputation: 31404

There are zero items in the GridView1.DataKeys collection.

That error message is means that you attempted to access outside the bounds of an array collection. There only place where it appears you are indexing into an array or collection is on GridView1.DataKeys

Upvotes: 1

dmay
dmay

Reputation: 1325

Check if GridView1.DataKeys is not epty. This error can only be caused by GridView1.DataKeys[0] operator on the line.

Upvotes: 1

Related Questions