C#学习教程:指数超出范围。 必须是非负数且小于集合的大小分享


指数超出范围。 必须是非负数且小于集合的大小

编译网格视图以显示一组订单时,我得到索引超出范围exception。

添加新行时不会发生这种情况,但是当我尝试删除或更新行时确实会发生这种情况。

任何帮助深表感谢。

设计师是:

                <asp:TextBox ID="txtOrderID" runat="server" Text=''>      <asp:Label ID="Label1" runat="server" Text=''>     <asp:TextBox ID="txtDate" runat="server" Text=''>      <asp:Label ID="Label2" runat="server" Text=''>     <asp:TextBox ID="txtOrderTaker" runat="server" Text=''>      <asp:Label ID="Label3" runat="server" Text=''>     <asp:TextBox ID="txtOrderBy" runat="server" Text=''>      <asp:Label ID="Label4" runat="server" Text=''>     <asp:TextBox ID="txtMaterial" runat="server" Text=''>      <asp:Label ID="Label5" runat="server" Text=''>     <asp:TextBox ID="txtTint" runat="server" Text=''>      <asp:Label ID="Label6" runat="server" Text=''>     <asp:TextBox ID="txtBCOR" runat="server" Text=''>      <asp:Label ID="Label7" runat="server" Text=''>     <asp:TextBox ID="txtPower" runat="server" Text=''>      <asp:Label ID="Label8" runat="server" Text=''>     <asp:TextBox ID="txtDiameter" runat="server" Text=''>      <asp:Label ID="LabelDiameter" runat="server" Text=''>     <asp:TextBox ID="txtDesign" runat="server" Text=''>      <asp:Label ID="Label9" runat="server" Text=''>     <asp:TextBox ID="txtNotes" runat="server" Text=''>      <asp:Label ID="Label10" runat="server" Text=''>      

代码隐藏是:

 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设置数据键的名称,但您的delete方法引用了DataKeys [e.RowIndex]。 我认为这是抛出exception的地方。

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

上述就是C#学习教程:指数超出范围。 必须是非负数且小于集合的大小分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1269695.html

(0)
上一篇 1天前
下一篇 1天前

精彩推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注