当前位置: 首页 > 面试题库 >

索引超出范围。必须为非负数并且小于集合的大小

卫琛
2023-03-14
问题内容

编译网格视图以显示一组订单时,出现索引超出范围异常。

当我添加新行时,它不会发生,但是当我尝试删除或更新行时,它会发生。

任何帮助深表感谢。

设计者是:

<asp:GridView   ID="GridView1" 
                        runat="server" 
                        AutoGenerateColumns="False" 
                        DataKeyNames="" 
                        OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                        OnRowEditing="GridView1_RowEditing" 
                        OnRowUpdating="GridView1_RowUpdating" 
                        OnRowCommand="GridView1_RowCommand" 
                        ShowFooter="True" 
                        OnRowDeleting="GridView1_RowDeleting">

            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                        <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="editButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:Button>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Order ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtOrderID" runat="server" Text='<%# Bind("OrderID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewOrderID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewDate" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Order Taken by">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtOrderTaker" runat="server" Text='<%# Bind("OrderTaker") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewOrderTaker" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("OrderTaker") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Order By">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtOrderBy" runat="server" Text='<%# Bind("OrderBy") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewOrderBy" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("OrderBy") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Lens Material">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtMaterial" runat="server" Text='<%# Bind("Material") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewMaterial" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Material") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tint">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTint" runat="server" Text='<%# Bind("Tint") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewTint" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Tint") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="BCOR">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBCOR" runat="server" Text='<%# Bind("BCOR") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewBCOR" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("BCOR") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Power">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPower" runat="server" Text='<%# Bind("Power") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewPower" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("Power") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Diameter">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewDiameter" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LabelDiameter" runat="server" Text='<%# Bind("Diameter") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Design">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDesign" runat="server" Text='<%# Bind("Design") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewDesign" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("Design") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Notes">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNotes" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewNotes" runat="server" TextMode="MultiLine"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label10" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
            </Columns>             
        </asp:GridView>

后面的代码是:

public class LensOrdersCls
{

    private string cnstr = ConfigurationManager.ConnectionStrings["PatientConnectionString"].ConnectionString;
    private string sql;

    public LensOrdersCls()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void Insert(int OrderID, string Date, string OrderTaker, string OrderBy, string Material, string Tint, string BCOR, string Power, string Diameter, string Design, string Notes)
    {
        string sql = "Insert Into LensOrders (OrderID, Date, OrderTaker, OrderBy, Material, Tint, BCOR, Power, Diameter, Design, Notes) Values ('" + OrderID
            + "' , '" + Date + "', '" + OrderTaker + "', '" + OrderBy + "', '" + Material + "' , '" + Tint + "' , '" + BCOR + "' , '" + Power + "', '" + Diameter + "', '" + Design + "', '" + Notes + "')";

        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }

    public DataTable Fetch()
    {
        string sql = "Select * From LensOrders";
        SqlDataAdapter da = new SqlDataAdapter(sql, cnstr);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }



    public void Update(int OrderID, string Date, string OrderTaker, string OrderBy, string Material, string Tint, string BCOR, string Power, string Diameter, string Design, string Notes)
    {
        string sql = "UPDATE LensOrders SET OrderID='" + OrderID + "', Date = '" + Date
            + "', OrderTaker='" + OrderTaker + "', OrderBy = '" + OrderBy + "' Material=" + Material + "' Tint= '" + Tint + "' BCOR= '" + BCOR + "' Power '" + Power + "' Diameter= '" + Diameter + "' Design= '" + Design + "' Notes= '" + Notes;
        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }

    public void Delete(int OrderID)
    {
        string sql = "Delete LensOrders Where OrderID=" + OrderID;
        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }

}

后面的页面代码是:

public partial class Default : System.Web.UI.Page
{
    LensOrdersCls LensOrder = new LensOrdersCls();

    private void FillOrdersInGrid()
    {
        DataTable dtOrder = LensOrder.Fetch();

        if (dtOrder.Rows.Count > 0)
        {
            GridView1.DataSource = dtOrder;
            GridView1.DataBind();
        }
        else
        {
            dtOrder.Rows.Add(dtOrder.NewRow());
            GridView1.DataSource = dtOrder;
            GridView1.DataBind();

            int TotalColumns = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GridView1.Rows[0].Cells[0].Text = "No Record Found";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            FillOrdersInGrid();
            test.Text = GridView1.Rows[0].Cells.Count.ToString();
        }
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtNewOrderID =     (TextBox)GridView1.FooterRow.FindControl("txtNewOrderID");
            TextBox txtNewDate =        (TextBox)GridView1.FooterRow.FindControl("txtNewDate");
            TextBox txtNewOrderTaker =  (TextBox)GridView1.FooterRow.FindControl("txtNewOrderTaker");
            TextBox txtNewOrderBy =     (TextBox)GridView1.FooterRow.FindControl("txtNewOrderBy");
            TextBox txtNewMaterial =    (TextBox)GridView1.FooterRow.FindControl("txtNewMaterial");
            TextBox txtNewTint =        (TextBox)GridView1.FooterRow.FindControl("txtNewTint");
            TextBox txtNewBCOR =        (TextBox)GridView1.FooterRow.FindControl("txtNewBCOR");
            TextBox txtNewPower =       (TextBox)GridView1.FooterRow.FindControl("txtNewPower");
            TextBox txtNewDiameter =    (TextBox)GridView1.FooterRow.FindControl("txtNewDiameter");
            TextBox txtNewDesign =      (TextBox)GridView1.FooterRow.FindControl("txtNewDesign");
            TextBox txtNewNotes =       (TextBox)GridView1.FooterRow.FindControl("txtNewNotes");

            LensOrder.Insert(Convert.ToInt32(txtNewOrderID.Text), 
                             txtNewDate.Text,
                             txtNewOrderTaker.Text, 
                             txtNewOrderBy.Text,
                             txtNewMaterial.Text,
                             txtNewTint.Text,
                             txtNewBCOR.Text,
                             txtNewPower.Text,
                             txtNewDiameter.Text,
                             txtNewDesign.Text,
                             txtNewNotes.Text);

            FillOrdersInGrid();
        }
    }

    // Row Editing
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        FillOrdersInGrid();
    }

    // Row Cancel editing
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillOrdersInGrid();
    }

    //Row updating
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtOrderID =    (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOrderID");
        TextBox txtDate =       (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDate");
        TextBox txtOrderTaker = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOrderTaker");
        TextBox txtOrderBy =    (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNewOrderBy");
        TextBox txtMaterial =   (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMaterial");
        TextBox txtTint =       (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTint");
        TextBox txtBCOR =       (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBCOR");
        TextBox txtPower =      (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPower");
        TextBox txtDiameter =   (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDiameter");
        TextBox txtDesign =     (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDesign");
        TextBox txtNotes =      (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNotes");

        LensOrder.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()),
                        txtDate.Text,
                        txtOrderTaker.Text,
                        txtOrderBy.Text,
                        txtMaterial.Text,
                        txtTint.Text,
                        txtBCOR.Text,
                        txtPower.Text,
                        txtDiameter.Text,
                        txtDesign.Text,
                        txtNotes.Text);

        GridView1.EditIndex = -1;
        FillOrdersInGrid();
    }

    //Deleting row

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        LensOrder.Delete(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString())); 
        FillOrdersInGrid();
    }
}

问题答案:

您没有为Grid设置数据键的名称,但是您的删除方法引用了DataKeys [e.RowIndex]。我认为这就是引发异常的地方。

在标记中设置DataKeyNames =“ OrderId”。



 类似资料:
  • 问题内容: 我已经开始处理我的第一个Java程序,它是一个简单的计算器,但是我得到一个错误,声称我的数组超出范围。我尝试对其进行调试,以了解这样做的原因和原因,并遵循纸上的代码,两者均显示了我期望和期望的结果。因此,我看不到问题出在哪里。代码不完整。 根据调试器,此行出现错误: 这是我当前拥有的代码的主要部分: 这是我收到的错误消息: 问题答案: 当等于中的最后一个元素索引时,则将大于最后一个元素

  • 所以我创建了这个注释: 但当我运行它时,我得到了这个异常: 无法为映射设置参数:ParameterMapping{Property='name',mode=in,javatype=class java.lang.String,JDBCType=varchar,NumericScale=null,ResultMapid='null',JDBCTypeName='null',Expression='n

  • 我有一个bean,我用它作为传输对象。bean的类定义为- 我正在使用这个bean,在带有Model属性注释的Spring控制器中。对于JSP,我有JSTL。我已经用这样的字段填充了JSP。 当我提交表单时,我得到Java . lang . indexoutofboundsexception:Index:0,Size: 0。

  • 你好,stackoverflow的好人们!我有一个奇怪的问题,我不明白。我要发布我的两个有问题的方法: 另一个: 所以,我正在根据我在网上找到的一些代码制作一个数独解算器。现在,当我试着调试程序时,我可以很好地检查一些行(并且它按照预期工作),但是一旦程序第一次在方法“resi”中调用“next”方法,它就会崩溃,出现数组索引越界异常。如果我只是尝试在不进行调试的情况下运行该程序,那么在同一方法调

  • 问题内容: 我有相同的错误此。就我而言,这是在应用尝试通过Google Cloud Client API上传文件时发生的。 我如何加载令牌是读取服务帐户json文件,并将其附加到PHP中。在过去的一个月中,它确实可以正常工作,因此我猜测Google改变了授权方式。 有没有人面对并解决过这个问题? 问题答案: 我大约在同一时间偶然发现了相同的问题,因此我期望使用Google的通用错误,但这是我的计算

  • 问题内容: 因此,我正在编写一个简单的程序来输入字符串并计算总数。的米 所以,这是我的代码 where 和str是我接受过的字符串,但是此错误不断出现 这是什么错误以及如何将其删除? 问题答案: 字符串,有效索引从0到n-1; 更改 至