索引超出了数组界限。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误:
行 212:
行 213: // 当数量被修改或者删除被确认时执行如下操作
行 214: if (quantity != (int)MyList.DataKeys[i] || jiecou != (decimal)MyList.DataKeyField[i] || sellcost != (decimal)MyList.DataKeyField[i] || remove.Checked == true)
行 215: {
行 216:
请问如何解决???
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误:
行 212:
行 213: // 当数量被修改或者删除被确认时执行如下操作
行 214: if (quantity != (int)MyList.DataKeys[i] || jiecou != (decimal)MyList.DataKeyField[i] || sellcost != (decimal)MyList.DataKeyField[i] || remove.Checked == true)
行 215: {
行 216:
请问如何解决???
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using DataAccess;namespace Lishi.Admin.Learn
{
/// <summary>
/// QuitHdsell 的摘要说明。
/// </summary>
public class QuitHdsell :Page
{
protected System.Web.UI.WebControls.Label MyError;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel4;
protected System.Web.UI.WebControls.DataGrid MyList;
protected System.Web.UI.WebControls.Label lblTotal;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Button updateshaoppingcart;
protected System.Web.UI.WebControls.Button nextstep;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.Panel DetailsPanel;
protected System.Web.UI.WebControls.Button Search;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
private string selnum1;
protected System.Web.UI.WebControls.TextBox bianma;
protected System.Web.UI.WebControls.Label selnum;
protected System.Web.UI.WebControls.Label Label1;
private string txtsearch;
protected System.Web.UI.WebControls.TextBox addnum;
protected System.Web.UI.WebControls.Label Label2;
private decimal sellcost;
private void Page_Load(object sender, System.EventArgs e)
{
selnum1=Request.Params["sellrecordnum"];
string bianma1=Request.Params["bianma"];
txtsearch=this.bianma.Text;
if (bianma1!="")
{
this.bianma.Text=bianma1;
}
this.selnum.Text="订单编号:" +selnum1;
//当页面是第一次加载时则读取数据库中的信息并显示
//也就是当是被PostBack的时候并不执行此操作
if (Page.IsPostBack == false)
{
PopulateShoppingCartList();
}
}
void PopulateShoppingCartList()
{ //新建订单的数据访问实例
ShoppingCartDB cart = new ShoppingCartDB();
// 获取订单ID
String cartID = selnum1;
SellrecordDB sellrecordstate=new SellrecordDB(); // 如果订单中无内容,则不显示面板并提示信息
if (cart.GetItemCount(cartID) == 0)
{
DetailsPanel.Visible = false;
MyError.Text = "您的订单内暂无商品";
//更新订单状态
string state="申请";
sellrecordstate.UpdateHdstate(selnum1,state);
}
else
{ // 将数据库中的信息绑定到DataList以显示信息
MyList.DataSource = cart.GetItems(cartID);
MyList.DataBind(); //更新总价信息
lblTotal.Text = String.Format( "{0:c}", cart.GetTotal(cartID));
//更新订单状态
string state="挂起";
sellrecordstate.UpdateHdstate(selnum1,state);
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Search.Click += new System.EventHandler(this.Search_Click);
this.MyList.SelectedIndexChanged += new System.EventHandler(this.MyList_SelectedIndexChanged);
this.updateshaoppingcart.Click += new System.EventHandler(this.updateshaoppingcart_Click);
this.nextstep.Click += new System.EventHandler(this.nextstep_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Search_Click(object sender, System.EventArgs e)
{
//获取商品ID
this.MyError.Visible=false;
SellrecordDB sellgoodsbypm=new SellrecordDB();
string goodsID=sellgoodsbypm.Searchgoodsbypm(txtsearch);
string cartID=selnum1;
string sellcoststate2;
//获取订单ID
if (goodsID ==null)
{
this.MyError.Text="找不到编码为<font color=blue>" + txtsearch + "</font>商品! ";
this.MyError.Visible=true;
}
else
{
if (txtsearch.Substring(0,1)=="1")
{
this.MyError.Text="<font color=blue>据我所知,乐思门市无水族类商品销售喔:)</font>";
this.MyError.Visible=true;
}
else
{ GoodsDB contact=new GoodsDB();
Goods goodsdetail = contact.GetgoodsDetail(Convert.ToInt32(goodsID));
Sellrecord sellcoststate1 = sellgoodsbypm.Getsellrecord(cartID);
sellcoststate2=sellcoststate1.sellcoststate.Trim();
int sellrecordid=sellcoststate1.sellrecordID;
{
sellcost=goodsdetail.goodspfcost;
}
else
{
sellcost=goodsdetail.goodscost;
}
this.MyError.Text=""; //添加购物信息到数据库,更新订单信息
ShoppingCartDB cartDB = new ShoppingCartDB();
cartDB.AddItem(cartID,Int32.Parse(goodsID),sellcost,Convert.ToInt32(this.addnum.Text),sellrecordid); //记录操作列表
string loginip=Request.UserHostName;
string record="订购" + txtsearch + "至" + selnum1;
string txtuser=Request.Cookies["FullName1"].Value.ToString();
AdminDB loginrecord=new AdminDB();
loginrecord.Loginrecord(txtuser,loginip,record); //重定向本页面,显示最新数据
Response.Redirect("QuitHdsell.aspx?sellrecordnum=" +selnum1);
}
}
} private void updateshaoppingcart_Click(object sender, System.EventArgs e)
{
//更新订单信息到数据库
this.MyError.Text="";
UpdateShoppingCartDatabase(); //更新显示
PopulateShoppingCartList();
}
private void UpdateShoppingCartDatabase()
{
//获取订单编号
string cartID=selnum1; ShoppingCartDB cartDB = new ShoppingCartDB(); // 依次更新DataList中的信息
for (int i=0; i < MyList.Items.Count; i++)
{ // 获得对应组件的引用
TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");
CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");
TextBox jiecoutxt = (TextBox) MyList.Items[i].FindControl("Selljiecou");
TextBox sellcosttxt = (TextBox) MyList.Items[i].FindControl("Sellcost"); // 为了防止用户输入有错,这里使用异常处理。
// 主要是看用户输入的数量是否为整数
// 如果不是整数就提示错误
int quantity;
decimal jiecou;
decimal sellcost;
try
{
quantity = Int32.Parse(quantityTxt.Text);
jiecou = decimal.Parse(jiecoutxt.Text);
sellcost = decimal.Parse(sellcosttxt.Text);
// 当数量被修改或者删除被确认时执行如下操作
Label lblProductID = (Label) MyList.Items[i].FindControl("orderlistid"); if (quantity == 0 || remove.Checked == true)
{
cartDB.RemoveItem(Int32.Parse(lblProductID.Text));
//记录操作列表
string loginip=Request.UserHostName;
string record="删除" + selnum1 + "的内容";
string txtuser=Request.Cookies["FullName1"].Value.ToString();
AdminDB loginrecord=new AdminDB();
loginrecord.Loginrecord(txtuser,loginip,record);
}
else
{
cartDB.UpdateItem(Int32.Parse(lblProductID.Text),quantity,jiecou,sellcost);
}
}
catch
{
MyError.Text = "您的一个或多个输入不正确";
}
}
} private void nextstep_Click(object sender, System.EventArgs e)
{
// 首先必须更新订单
UpdateShoppingCartDatabase(); // 如果订单不空,则提交给Checkout.aspx处理
ShoppingCartDB cart = new ShoppingCartDB(); // 获取订单ID
String cartId=selnum1; // If the cart isn't empty, navigate to checkout page
if (cart.GetItemCount(cartId) !=0)
{
Response.Redirect("HdCheckout.aspx?sellrecordnum=" +selnum1);
}
else
{
MyError.Text = "订单为空,不能结帐!";
}
} private void MyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
MyList.DataKeyField[i]
MyList.DataKeyField[i]
不存在啊