Click here to Skip to main content
15,898,918 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
when i tried to delete or edit from my grid view it show me this error
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

is there anything wrong with my codes?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CustomerView : System.Web.UI.Page
{
    Customer aCustomer = new Customer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    protected void bind()
    {
        List<customer> customerList = new List<customer>();
        customerList = aCustomer.getCustomerAll();
        gvCustomer.DataSource = customerList;
        gvCustomer.DataBind();
    }

    protected void gvCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvCustomer.SelectedRow;
        string CustID = row.Cells[0].Text;
        Response.Redirect("CustomerDetails.aspx?StaffID=" + CustID);
    }
    protected void gvCustomer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int result = 0;
        Customer customer = new Customer();
        string CustID = gvCustomer.DataKeys[e.RowIndex].Value.ToString();
        result = customer.CustomerDelete(CustID);

        if (result > 0)
        {
            Response.Write("<script>alert('Customer Remove Sucessfully');</script>");
        }
        else
        {
            Response.Write("<script>alert('Customer Removal Not Sucessful');</script>");
        }

        Response.Redirect("CustomerView.aspx");
    }
    protected void gvCustomer_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCustomer.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void gvCustomer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCustomer.EditIndex = -1;
        bind();
    }
    protected void gvCustomer_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int result = 0;
        Customer customer = new Customer();
        GridViewRow row = (GridViewRow)gvCustomer.Rows[e.RowIndex];
        string id = gvCustomer.DataKeys[e.RowIndex].Value.ToString();
        string tCustID = ((TextBox)row.Cells[0].Controls[0]).Text;
        string tCName = ((TextBox)row.Cells[1].Controls[0]).Text;
        string tCGender = ((TextBox)row.Cells[2].Controls[0]).Text;
        string tCDob = ((TextBox)row.Cells[3].Controls[0]).Text;
        string tCAddress = ((TextBox)row.Cells[4].Controls[0]).Text;
        string tCTelNo = ((TextBox)row.Cells[5].Controls[0]).Text;
        string tCEmail = ((TextBox)row.Cells[6].Controls[0]).Text;
        string tCPassword = ((TextBox)row.Cells[7].Controls[0]).Text;
        string tMailingList = ((TextBox)row.Cells[8].Controls[0]).Text;
        string tCUsername = ((TextBox)row.Cells[9].Controls[0]).Text;
        result = customer.CustomerUpdate(tCustID, tCName, tCGender, tCDob, tCAddress, tCTelNo, tCEmail, tCPassword, tMailingList, tCUsername);
        if (result > 0)
        {
            Response.Write("<script>alert('Customer updated successfully');</script>");
        }
        else
        {
            Response.Write("<script>alert('Customer NOT updated');</script>");
        }
        gvCustomer.EditIndex = -1;
        bind();
    }
    protected void btn_AddNewCustomer_Click(object sender, EventArgs e)
    {
        Response.Redirect("CustomerInsert.aspx");
    }
}
Posted
Updated 25-Jul-15 21:34pm
v3
Comments
Wendelius 26-Jul-15 3:35am    
When you debug the application, on what line the exception occurs?
WinterBanana 26-Jul-15 9:31am    
string CustID = gv_Customer.DataKeys[e.RowIndex].Value.ToString();

Without knowing where the error occurs, we can't help you much, if at all. Even then, we need to know what it happening, and when, and what values you get before we could tell you "it's that". And we can't run your code, and we don't have your data!

So start with the debugger, and look at what exactly is going on. put a breakpoint at the start of each method, and use the debugger to look at what the index values are, and how big the objects they are indexing into are. When you know which index is giving the problem, you can start looking at why it's wrong - but since that's likely to be related to your data, we can't help you!
 
Share this answer
 
Just a start in gvCustomer_SelectedIndexChanged you just assume that a valid row has been selected, for all selection changes on controls always validate before using.
 
Share this answer
 
v3
The Debugger is your friend.
Track the value of your index, you may get surprises.
 
Share this answer
 

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