protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // try // { this.GridView1.DataBind(); Model.MallUser userCurrent = (Model.MallUser)Session["user"]; string UserName = userCurrent.UserName; string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("/App_Data/bbs.mdb"); OleDbConnection oleConnection = new OleDbConnection(ConnStr); DataTable dt1 = new DataTable(); oleConnection.Open(); string sql = "select TotalPrice,UserName from Cart where UserName ='******'"; OleDbDataAdapter OldDat1 = new OleDbDataAdapter(sql, oleConnection); OldDat1.Fill(dt1); oleConnection.Close(); DataRow[] dr = dt1.Select("UserName =UserName"); float TotalPricezong = 0; for (int i = 0; i < dr.Length; i++) { //TotalPricezong += Convert.ToInt32(dr[i]["TotalPrice"]); TotalPricezong += Convert.ToSingle(dr[i]["TotalPrice"]); TotalPrice.Text = Convert.ToString(TotalPricezong); } // } // catch (Exception ee) // { // string ex = ee.Message; // } } // string GoodsID=Session["GoodsID"].ToString(); }
protected void btnPay_Click(object sender, EventArgs e) { Model.MallUser userCurrent = (Model.MallUser)Session["user"]; string UserName = userCurrent.UserName; deliver(UserName); // Response .Redirect ("Order.aspx"); }
public void deliver(int GoodsID) { string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("/App_Data/bbs.mdb"); OleDbConnection oleConnection = new OleDbConnection(ConnStr); DataTable dt1 = new DataTable(); Carts c = new Carts(); oleConnection.Open(); string sql = "select Picture,GoodsID,GoodsName,GoodsFrom,Introduction,NowPrice from goods where GoodsID = " + GoodsID + ""; OleDbDataAdapter OldDat1 = new OleDbDataAdapter(sql, oleConnection); OldDat1.Fill(dt1); c.Picture = dt1.Rows[0][0].ToString(); c.GoodsID = dt1.Rows[0][1].ToString(); c.GoodsName = dt1.Rows[0][2].ToString(); c.GoodsFrom = dt1.Rows[0][3].ToString(); c.Introduction = dt1.Rows[0][4].ToString(); c.NowPrice = float.Parse(dt1.Rows[0][5].ToString()); Model.MallUser userCurrent = (Model.MallUser)Session["user"]; string a = userCurrent.UserName; oleConnection.Close(); //判断当前购物车是否已有该商品,如果有则amount+1,若没有则新添一列 OleDbCommand comm = oleConnection.CreateCommand(); comm.CommandType = CommandType.Text; comm.CommandText = "select count(*) from Cart where GoodsID = " + GoodsID + ""; OleDbParameter[] param = { new OleDbParameter("@GoodsID", c.GoodsID) }; comm.Parameters.AddRange(param); OleDbCommand myCommand = new OleDbCommand(comm.CommandText, oleConnection); myCommand.Connection.Open(); int flag = 1; flag = (int)comm.ExecuteScalar(); myCommand.Connection.Close(); if (flag == 0) //若没有则新添一列 { c.Amount = 1; c.TotalPrice = c.NowPrice; string str = "insert into Cart(UserName,Amount,TotalPrice,Picture,GoodsID,GoodsName,GoodsFrom,Introduction,NowPrice) values ('" + a + "','" + c.Amount + "','" + c.TotalPrice + "','" + c.Picture + "'," + c.GoodsID + ",'" + c.GoodsName + "','" + c.GoodsFrom + "','" + c.Introduction + "'," + c.NowPrice + ")"; OleDbCommand myIns = new OleDbCommand(str, oleConnection); myIns.Connection.Open(); int ni = myIns.ExecuteNonQuery(); myIns.Connection.Close(); oleConnection.Close(); if (ni > 0) { Response.Write("<script language=javascript>alert('已加入购物车!');</script>"); } else { Response.Write("<script language=javascript>alert('错误!');</script>"); } } else //若已有此商品,则把此商品对应的amount取出来,+1,再存进此列 { DataTable dt2 = new DataTable(); oleConnection.Open(); string sql2 = "select Amount,NowPrice,TotalPrice from Cart where GoodsID = " + GoodsID + ""; OleDbDataAdapter OldDat2 = new OleDbDataAdapter(sql2, oleConnection); OldDat2.Fill(dt2); c.Amount = Int32.Parse(dt2.Rows[0][0].ToString()); c.NowPrice = float.Parse(dt2.Rows[0][1].ToString()); c.TotalPrice = float.Parse(dt2.Rows[0][2].ToString()); c.Amount += 1; c.TotalPrice = c.Amount * c.NowPrice; // c.TotalPrice = c.TotalPrice + c.NowPrice; oleConnection.Close(); string str = "update Cart set Amount = '" + c.Amount + "',TotalPrice='" + c.TotalPrice + " ' where GoodsID = " + GoodsID + ""; OleDbCommand myIns = new OleDbCommand(str, oleConnection); myIns.Connection.Open(); int ni = myIns.ExecuteNonQuery(); myIns.Connection.Close(); if (ni > 0) { Response.Write("<script language=javascript>alert('已加入购物车!');</script>"); } else { Response.Write("<script language=javascript>alert('错误!');</script>"); } } }