public void SepettenUrunCikart(int id)
        {
            //List<Product> urunler = new List<Product>();
            if (Session["AktifSepet"] != null)
            {
                Sepet s = (Sepet)Session["AktifSepet"];

                SepetDetay detay = s.Urunler.Find(x => x.Urun.ProductID == id);

                s.Urunler.Remove(detay);

                Session["AktifSepet"] = s;
                //urunler = s.Urunler;
            }
        }
        public IActionResult CheckOut()
        {
            Models.ShoppingCart cart = new Models.ShoppingCart();
            string json        = HttpContext.Session.GetString("shoppingCart");
            var    productList = JsonConvert.DeserializeObject <Models.ShoppingCart>(json);

            Dictionary <string, int> newDictionary = new Dictionary <string, int>();
            var productCount = 0;
            Dictionary <int, int> dictionaryCounts = new Dictionary <int, int>();
            Sepet sepet = new Sepet();

            foreach (int i in productList.ProductsIdList)
            {
                int count;
                if (dictionaryCounts.TryGetValue(i, out count))
                {
                    dictionaryCounts[i] = count + 1;
                    productCount++;
                    newDictionary[_products.GetTitle(i)] = count + 1;
                }
                else
                {
                    dictionaryCounts.Add(i, 1);
                    var productId = productList.ProductsIdList[0];
                    newDictionary.Add(_products.GetTitle(i), 1);
                }
            }

            foreach (var i in dictionaryCounts)
            {
                Product product = new Product();
                product = _products.GetProduct(i.Key);
                SepetDetay sptDetay = new SepetDetay();
                sptDetay.product           = new ProductDetailModel();
                sptDetay.product.ProductId = product.Id;
                sptDetay.product.Title     = product.Title;
                sptDetay.product.Price     = product.Price;
                sptDetay.Quantity          = i.Value;
                sepet.sepetDetayList.Add(sptDetay);
            }
            return(View(sepet));
        }
        protected void BtnAddCard_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] == null && Session["UserID"] == null)
            {
                Response.Redirect("~/SignIn.aspx");
            }
            else
            {
                id = Convert.ToInt32(Request.QueryString["id"]);//product id aldık
                Product       product    = new Product();
                Sepet         sepet      = new Sepet();
                SepetDetay    sepetDetay = new SepetDetay();
                SqlConnection con        = new SqlConnection(ConnectString);
                con.Open();
                SqlCommand komut = new SqlCommand("select * from Table_Product where id = @id", con);
                komut.Parameters.AddWithValue("@id", id);
                SqlDataReader rdr = komut.ExecuteReader();
                if (rdr.Read())
                {
                    product.Id                 = Convert.ToInt32(rdr[0]);
                    product.ProductName        = rdr[1].ToString();
                    product.ProductDescription = rdr[2].ToString();
                    product.ProductPrice       = Convert.ToInt32(rdr[3]);
                    product.ProductImages      = rdr[4].ToString();
                    product.Category           = Convert.ToInt32(rdr[5]);
                    product.Brand              = Convert.ToInt32(rdr[6]);
                }
                rdr.Close();
                int        count = 0;
                SqlCommand cmd1  = new SqlCommand("select * from Sepet where KullaniciId=@userid", con);
                cmd1.Parameters.AddWithValue("@userid", Convert.ToInt32(Session["UserID"]));
                SqlDataReader reader = cmd1.ExecuteReader();
                while (reader.Read())
                {
                    sepet.Durum = Convert.ToBoolean(reader[4]);
                    count++;
                }
                reader.Close();
                if (count == 0 || sepet.Durum == true)
                {
                    SqlCommand sqlCommand = new SqlCommand("insert into Sepet (SepetTutari,OlusturmaTarihi,KullaniciId,Durum) values(@tutar,@tarih,@userid,@d)", con);
                    sqlCommand.Parameters.AddWithValue("@tutar", 0);
                    sqlCommand.Parameters.AddWithValue("@tarih", DateTime.Now.ToString());
                    sqlCommand.Parameters.AddWithValue("@userid", Convert.ToInt32(Session["UserID"]));
                    sqlCommand.Parameters.AddWithValue("@d", false);
                    sqlCommand.ExecuteNonQuery();
                }

                SqlCommand sql = new SqlCommand("select * from Sepet where KullaniciId=@ui and Durum=@d", con);
                sql.Parameters.AddWithValue("@ui", Convert.ToInt32(Session["UserID"]));
                sql.Parameters.AddWithValue("@d", false);
                SqlDataReader reader1 = sql.ExecuteReader();
                if (reader1.Read())
                {
                    sepet.id          = Convert.ToInt32(reader1[0]);
                    sepet.SepetTutari = Convert.ToDecimal(reader1[1]);
                }
                reader1.Close();

                SqlCommand sqlcommand = new SqlCommand("update Sepet set SepetTutari=@ürünyeni where KullaniciId=@ui  and Durum=@d", con);
                sqlcommand.Parameters.AddWithValue("@ui", Convert.ToInt32(Session["UserID"]));
                sqlcommand.Parameters.AddWithValue("@d", false);
                sqlcommand.Parameters.AddWithValue("@ürünyeni", product.ProductPrice + sepet.SepetTutari);
                //sqlcommand.ExecuteNonQuery();
                SqlDataReader reader2 = sqlcommand.ExecuteReader();
                if (reader2.Read())
                {
                    sepet.id = Convert.ToInt32(reader2[0]);
                }
                reader2.Close();
                sqlcommand.ExecuteNonQuery();

                int        sip    = sepet.id;
                SqlCommand cmd123 = new SqlCommand("select * from SepetDetay where UrunId=@urunid and SepetId=@sepetid", con);//ürrün kaç tane kontrol için
                cmd123.Parameters.AddWithValue("@urunid", product.Id);
                cmd123.Parameters.AddWithValue("@sepetid", sepet.id);
                SqlDataReader dataReader = cmd123.ExecuteReader();
                if (dataReader.Read())
                {
                    sepetDetay.UrunId = Convert.ToInt32(dataReader[2]);
                    sepetDetay.Adet   = Convert.ToInt32(dataReader[4]);
                }
                dataReader.Close();
                if (sepetDetay.UrunId != product.Id)
                {
                    SqlCommand sqlcommand1 = new SqlCommand("insert into SepetDetay (SepetId,UrunId,Fiyat,Adet) values (@SepetId, @UrunId, @Fiyat, 1)", con);
                    sqlcommand1.Parameters.AddWithValue("@SepetId", sip);
                    sqlcommand1.Parameters.AddWithValue("@UrunId", product.Id);
                    sqlcommand1.Parameters.AddWithValue("@fiyat", product.ProductPrice);
                    sqlcommand1.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand sqlcommand2 = new SqlCommand("update SepetDetay  set Fiyat=@ürünfiyat, Adet=@adet where SepetId=@sepetid  ", con);
                    sqlcommand2.Parameters.AddWithValue("@ürünfiyat", product.ProductPrice * sepetDetay.Adet);
                    sqlcommand2.Parameters.AddWithValue("@adet", sepetDetay.Adet + 1);
                    sqlcommand2.Parameters.AddWithValue("@sepetid", sepet.id);
                    sqlcommand2.ExecuteNonQuery();
                }



                con.Close();
                Response.Redirect("~/Cart.aspx");
            }
        }
Example #4
0
        protected void BtnRemoveCard_Click(object sender, EventArgs e)
        {
            Product       product    = new Product();
            Sepet         sepet      = new Sepet();
            Button        btn        = (Button)(sender);
            string        pid        = btn.CommandArgument;
            SepetDetay    sepetDetay = new SepetDetay();
            SqlConnection con        = new SqlConnection(ConnectString);

            con.Open();
            SqlCommand command = new SqlCommand("select * from Sepet where KullaniciId=@ui and Durum=@d", con);

            command.Parameters.AddWithValue("@ui", Convert.ToInt32(Session["UserID"]));
            command.Parameters.AddWithValue("@d", false);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                sepet.id          = Convert.ToInt32(reader[0]);
                sepet.sepettutarı = Convert.ToDecimal(reader[1]);
            }
            CarTotalSpan.InnerText = sepet.sepettutarı.ToString();
            reader.Close();
            SqlCommand cmd1 = new SqlCommand("select * from SepetDetay where UrunId=@urunid", con);//ürün kaç tane kontrol için

            cmd1.Parameters.AddWithValue("@urunid", product.id);
            SqlDataReader dataReader = cmd1.ExecuteReader();

            if (dataReader.Read())
            {
                sepetDetay.ürünid = Convert.ToInt32(dataReader[2]);
                sepetDetay.adet   = Convert.ToInt32(dataReader[4]);
            }
            dataReader.Close();
            SqlCommand komut = new SqlCommand("select * from Table_Product where id = @id", con);

            komut.Parameters.AddWithValue("@id", pid);
            SqlDataReader rdr = komut.ExecuteReader();

            if (rdr.Read())
            {
                product.id                 = Convert.ToInt32(rdr[0]);
                product.ProductName        = rdr[1].ToString();
                product.ProductDescription = rdr[2].ToString();
                product.ProductPrice       = Convert.ToInt32(rdr[3]);
                product.ProductImages      = rdr[4].ToString();
                product.Category           = Convert.ToInt32(rdr[5]);
                product.Brand              = Convert.ToInt32(rdr[6]);
            }
            rdr.Close();
            decimal    ara        = sepet.sepettutarı - (product.ProductPrice * sepetDetay.adet);
            SqlCommand sqlcommand = new SqlCommand("update Sepet set SepetTutari=@ürünyeni where KullaniciId=@ui and Durum=@d", con);

            sqlcommand.Parameters.AddWithValue("@ui", Convert.ToInt32(Session["UserID"]));
            sqlcommand.Parameters.AddWithValue("@d", false);
            sqlcommand.Parameters.AddWithValue("@ürünyeni", Convert.ToDecimal(ara));
            sqlcommand.ExecuteNonQuery();

            SqlCommand cmd = new SqlCommand("delete  from SepetDetay where UrunId=@ürünid and SepetId=@sepetid", con);

            cmd.Parameters.AddWithValue("@ürünid", pid);
            cmd.Parameters.AddWithValue("@sepetid", sepet.id);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("~/Products.aspx");
        }