ASP.net用Session做购物车的问题
用二位数组储存购物商品和价格两个信息储存在Session中,在商品页面点加入购物车,商品成功加入 但是点其他商品加入购物车以后数组就会清空永远只有一个信息,请问问题出在那?
public partial class _Default : System.Web.UI.Page
{
SqlDataReader sdr;
string[,] ary = new string[50, 2];
protected void Page_Load(object sender, EventArgs e)
{
Session.Timeout = 6000;
sltpdinfo();
}
protected void sltpdinfo()
{
SqlConnection scn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='d:\\My Documents\\Visual Studio 2005\\WebSites\\b2cweb\\App_Data\\b2cdatabase.mdf';Integrated Security=True;User Instance=True");
SqlCommand sltpd = new SqlCommand("select pcode,pname,pscprice from product where pcode=@pcode",scn);
scn.Open();
SqlParameter pcode = new SqlParameter("pcode",Request.QueryString["pcode"]);
sltpd.Parameters.Add(pcode);
sdr = sltpd.ExecuteReader();
sdr.Read();
}
protected void btnpay_Click(object sender, EventArgs e)
{
}
protected void btnadd_Click(object sender, EventArgs e)
{
string[,] ary = new string[50, 2];
Session["pcode"] = sdr[0];
Session["pname"] = sdr[1];
Session["pscprice"] = sdr[2];
Session["mycar"] = ary;
if (ary[0,0]==null)
{
ary[0,0] = Session["pname"].ToString();
ary[0,1] = Session["pscprice"].ToString();
}
else
{
for (int i = 0; i < 50; i++)
{
if (ary[i, 0] == Session["pname"].ToString())
{
ary[i + 1, 0] = Session["pname"].ToString();
ary[i + 1, 1] = Session["pscprice"].ToString();
}
}
}
Response.Redirect("~/shopcar.aspx");
}
}