Ejemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GridViewCartDetails == null)
        {
            ButBuy.Enabled = false;
        }
        if (!IsPostBack)
        {
            string username = User.Identity.Name;
            if (!String.IsNullOrEmpty(username))
            {
                ButLogin.Visible = false;
                ButBuy.Enabled   = true;
            }

            try
            {
                int        noofbooks     = (int)Session["count"];
                List <int> selectedbooks = new List <int>();

                for (int i = 0; i < noofbooks; i++)
                {
                    selectedbooks.Add((int)Session["BookNo" + i.ToString()]);
                }

                BookshopEntities context = new BookshopEntities();

                cartBookList = context.Books.Where(x => selectedbooks.Contains(x.BookID)).ToList();

                foreach (Book bc in cartBookList)
                {
                    bc.Stock = 0;
                    foreach (int i in selectedbooks)
                    {
                        if (i == bc.BookID)
                        {
                            bc.Stock++;
                        }
                    }
                }
            }
            catch (NullReferenceException nue)
            {
                LabErrorMsg.Text = String.Format("You currently do not have any cart items. Please continue shopping!");
            }

            //for finding datasource from Session State
            GridViewCartDetails.DataSource = cartBookList.ToList <Book>();
            GridViewCartDetails.DataBind();

            ViewCartLogic vcl = new ViewCartLogic();

            LabSummaryCart.Text = String.Format("You have {0} items for a total of {1:c} in your cart.", vcl.CountCart(cartBookList), vcl.CountPrice(cartBookList));
            LabTotalPrice.Text  = vcl.CountPrice(cartBookList).ToString();
        }
    }
Ejemplo n.º 2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string username = User.Identity.Name;
            if (!String.IsNullOrEmpty(username))
            {
                ButLogin.Visible = false;
                ButBuy.Visible   = true;
            }
        }

        try
        {
            int        noofbooks     = (int)Session["count"];
            List <int> selectedbooks = new List <int>();

            for (int i = 0; i < noofbooks; i++)
            {
                selectedbooks.Add(Convert.ToInt32(Session["BookNo" + i.ToString()]));
            }

            cartBookList = context.Books.Where(x => selectedbooks.Contains(x.BookID)).ToList();

            foreach (Book bc in cartBookList)
            {
                bc.Stock = 0;
                foreach (int i in selectedbooks)
                {
                    if (i == bc.BookID)
                    {
                        bc.Stock++;
                    }
                }
            }
        }
        catch (NullReferenceException nue)
        { }

        List <CartModel> books = new List <CartModel>();

        foreach (Book a in cartBookList)
        {
            CartModel b         = new CartModel();
            Book      foundBook = context.Books.Where(x => x.BookID == a.BookID).ToList().First();
            b.ISBN = foundBook.ISBN;
            //b.Price = Double.Parse(foundBook.Price.ToString());
            b.Price = Math.Round((double)(foundBook.Price * (100 - foundBook.Discount)) * 0.01, 2);
            b.Stock = a.Stock;
            b.Title = foundBook.Title;
            books.Add(b);
        }

        double        totalPrice = 0;
        StringBuilder htmlWriter = new StringBuilder();

        foreach (CartModel tran in books)
        {
            htmlWriter.Append("<div class='card'>");
            htmlWriter.Append("<div class='image'>");
            htmlWriter.Append("<img alt='Image Unavailable' src='/Team13BookShop/images/" + tran.ISBN + ".jpg' />");
            htmlWriter.Append("</div>");
            htmlWriter.Append("<div class='content'>");
            htmlWriter.Append("<div class='header'>" + tran.Title + "</div>");
            htmlWriter.Append("<div class='meta'>");
            htmlWriter.Append("<a>Prices: " + tran.Price + "</a>");
            htmlWriter.Append("</div>");
            htmlWriter.Append("</div>");
            htmlWriter.Append("<div class='extra content'>");
            htmlWriter.Append("<span class='right floated'>Total Price: " + tran.Stock * tran.Price + "</span>");
            htmlWriter.Append("<span>Quantity: " + tran.Stock + "</span>");
            htmlWriter.Append("</div>");
            htmlWriter.Append("</div>");
            totalPrice += tran.Stock * tran.Price;
        }
        PlaceHolder1.Controls.Add(new Literal {
            Text = htmlWriter.ToString()
        });
        InputTotalPrice.Value = "$ " + totalPrice.ToString();

        ViewCartLogic vcl = new ViewCartLogic();

        TextBoxCartSummary.Text = String.Format("You have {0} book title(s) for a total of {1:c} in your cart.", vcl.CountCart(cartBookList), vcl.CountPrice(cartBookList));
    }