Пример #1
0
        public void dbInsertShoppingCart(ShoppingCartDetails shoppingCart)
        {
            var groceryAmount = dbShopingCartExicst(shoppingCart);

            if (groceryAmount != -1)
            {
                float proslediAmount = shoppingCart.Amount + groceryAmount;
                dbUpdateShoppingCart(proslediAmount, shoppingCart.Grocery.Name, shoppingCart.GroupID);
            }
            else
            {
                if (con.State != System.Data.ConnectionState.Open)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand("INSERT INTO [ShoppingCart] (Amount,GroceryName,GroupID) VALUES(@Amount,@GroceryName,@GroupID)", con);
                cmd.Parameters.Add("@Amount", SqlDbType.Float).Value        = shoppingCart.Amount;
                cmd.Parameters.Add("@GroceryName", SqlDbType.VarChar).Value = shoppingCart.Grocery.Name;
                cmd.Parameters.Add("@GroupID", SqlDbType.VarChar).Value     = shoppingCart.GroupID;
                int test = cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            con.Close();
        }
Пример #2
0
        public float dbShopingCartExicst(ShoppingCartDetails shoppingCart)
        {
            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }
            SqlCommand command = new SqlCommand("SELECT * FROM [ShoppingCart] WHERE GroceryName=@GroceryName AND GroupID=@GroupID", con);

            command.Parameters.Add("@GroceryName", SqlDbType.VarChar).Value = shoppingCart.Grocery.Name;
            command.Parameters.Add("@GroupID", SqlDbType.VarChar).Value     = shoppingCart.GroupID;
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read() == true)
            {
                float prosledi = float.Parse(reader.GetValue(0).ToString());
                reader.Close();
                command.Dispose();
                con.Close();
                return(prosledi);
            }

            reader.Close();
            command.Dispose();
            con.Close();
            return(-1);
        }
Пример #3
0
        public List <ShoppingCartDetails> dbGetShoppingCart(string id)
        {
            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }
            List <ShoppingCartDetails> listShoppingCartDetails = new List <ShoppingCartDetails>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM [ShoppingCart] INNER JOIN [Groceries] ON ShoppingCart.GroceryName=Groceries.Name WHERE GroupID=@id", con);

            cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar).Value = id;
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                ShoppingCartDetails shoppingCart = new ShoppingCartDetails();
                GroceryDetails      grocery      = new GroceryDetails();
                shoppingCart.Amount  = float.Parse(reader.GetValue(0).ToString());
                shoppingCart.GroupID = reader.GetValue(2).ToString();
                grocery.Name         = reader.GetValue(3).ToString();
                grocery.Image        = null;
                grocery.Unit         = reader.GetValue(5).ToString();
                grocery.Category     = reader.GetValue(6).ToString();
                shoppingCart.Grocery = grocery;
                listShoppingCartDetails.Add(shoppingCart);
            }
            reader.Close();
            cmd.Dispose();
            con.Close();
            return(listShoppingCartDetails);
        }