我该如何解决这个错误 [英] How Do I Solve The Error

查看:81
本文介绍了我该如何解决这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从我的网格视图中删除或编辑时,它会向我显示此错误

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?

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");
    }
}

推荐答案

如果不知道错误发生在哪里,我们无法帮到你,如果一点都不即便如此,我们还需要知道它发生了什么,何时以及在我们告诉你它就是那个之前你得到了什么价值。我们无法运行您的代码,而且我们没有您的数据!



所以从调试器开始,看看到底发生了什么。在每个方法的开头放置一个断点,并使用调试器查看索引值是什么,以及它们索引的对象有多大。当您知道哪个索引出现问题时,您可以开始查看错误原因 - 但由于这可能与您的数据有关,我们无法帮助您!
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!


只是一个从gvCustomer_SelectedIndexChanged开始,你只是假设选择了一个有效的行,因为控件上的所有选择更改总是在使用前验证。
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.


调试器是你的朋友。

跟踪索引的价值,您可能会感到意外。
The Debugger is your friend.
Track the value of your index, you may get surprises.


这篇关于我该如何解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆