Example #1
0
        protected void AddBook(int id)
        {
            //isExit表示商品是否已经在购物车中,是为1,否为0
            int isExit = 0;

            for (int j = 0; j < Profile.Cart.BookName.Count; j++)
            {
                if (id == (int)Profile.Cart.BookId[j])
                {
                    //若同类商品已经存在于购物车,则将该商品的购买数量加1
                    int s = (int)Profile.Cart.Qty[j];
                    s++;
                    Profile.Cart.Qty[j] = s;
                    Profile.Save();
                    isExit = 1;
                }
            }
            if (isExit == 0)
            {
                //若购物车中无指定商品编号的商品,添加一个新商品到Profile.cart的个属性
                MyBookStoreDataContext db = new MyBookStoreDataContext();
                var book = (from p in db.Book
                            where p.BookId == id
                            select p).First();
                Profile.Cart.Price.Add(book.Price);
                Profile.Cart.Qty.Add(1);
                Profile.Cart.BookId.Add(book.BookId);
                Profile.Cart.BookName.Add(book.BookName);
                Profile.Save();//保存到ASPNETDB数据库中
            }
        }
Example #2
0
        protected void Button3_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            MyBookStoreDataContext db = new MyBookStoreDataContext();

            //循环利用FindControl()找到TextBox控件txtQty,然后判断其值是否为空,若非空
            //则在Book表中查询txtQty所在行商品编号确定的商品,从而比较txtQty中的输入值和库存量
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                TextBox txtQty = new TextBox();
                txtQty = (TextBox)GridView1.Rows[i].FindControl("txtQty");
                if (txtQty != null)
                {
                    var book = (from p in db.Book
                                where p.BookId == int.Parse(GridView1.Rows[i].Cells[1].Text)
                                select p).First();
                    if (int.Parse(txtQty.Text) > book.Qty)
                    {
                        lblError.Text += "Erro:库存不足,商品名为" + GridView1.Rows[i].Cells[2].Text + "的商品数量为" + book.Qty.ToString() + "<br/>";
                    }
                    else
                    {
                        //调用自定义方法,改变存储在Profile中的购买数量
                        ChangeQty(int.Parse(GridView1.Rows[i].Cells[1].Text), int.Parse(txtQty.Text));
                    }
                }
            }
            Bind(); //调用自定义方法,显示购物车中的所有商品
        }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyBookStoreDataContext db = new MyBookStoreDataContext();
        var result = from c in db.Book
                     where SqlMethods.Like(c.BookName, "%" + TextBox1.Text + "%")
                     select c;

        if (result.Count() != 0)
        {
            DetailsView1.DataSource = result;
            DetailsView1.DataBind();
        }
        else
        {
            Label2.Text = "没有满足条件的数据";
        }
    }
Example #4
0
    protected void Button2_Click(object sender, EventArgs e)
    {
        MyBookStoreDataContext db = new MyBookStoreDataContext();
        //在AdminOrder表中添加一条订单
        AdminOrder order = new AdminOrder();

        //收货人、联系方式、收货地址从页面的TextBox控件中获得
        order.UserNmae  = txtName.Text;
        order.Address   = txtAddress.Text;
        order.Phone     = txtPhone.Text;
        order.OrderDate = DateTime.Now;
        order.Status    = "未审核";
        //插入实体order,提交更改后在AdminOrder表添加一条记录
        db.AdminOrder.InsertOnSubmit(order);
        db.SubmitChanges();
        //在OrderItem表中添加订单详细信息
        int id = order.OrderId;

        for (int i = 0; i < Profile.Cart.BookName.Count; i++)
        {
            OrderItem orderItem = new OrderItem();
            orderItem.OrderId    = id;
            orderItem.BookName   = (string)Profile.Cart.BookName[i];
            orderItem.Price      = (decimal)Profile.Cart.Price[i];
            orderItem.Qty        = (int)Profile.Cart.Qty[i];
            orderItem.TotalPrice = (int)Profile.Cart.Qty[i] * (decimal)Profile.Cart.Price[i];
            db.OrderItem.InsertOnSubmit(orderItem);
            db.SubmitChanges();

            var book = (from c in db.Book
                        where c.BookId == (int)Profile.Cart.BookId[i]
                        select c).First();
            book.Qty -= orderItem.Qty;
            db.SubmitChanges();
        }
        //清空Profile.Cart中的对象
        Profile.Cart.BookId.Clear();
        Profile.Cart.BookName.Clear();
        Profile.Cart.Price.Clear();
        Profile.Cart.Qty.Clear();
        Profile.Cart.TotalPrice = "";
        lblMsg.Text             = "已经结算成功,谢谢光临";
    }