Problem to refresh quantity shopping cart C #

0

When I refresh the quantity already in the shopping cart, I can only refresh the quantity of a product, if I have 2 products in the cart I can not refresh one of them.

Imagine that the first product has 5 units and the 2nd product has 1 unit, if we want to change the quantity of the 2nd product to 4 units is not possible, it is only possible for 5 units, that is if you want to change the quantity of one product, all products have to have the same quantity number. I tried several ways but I could not resolve the error.

Code:

ImageButton lnksender = (ImageButton)sender;
            Session["IDCarrinho"] = Convert.ToInt32(lnksender.CommandArgument);
            try
            {
                foreach (GridViewRow di in GridViewManage.Rows)
                {
                    TextBox txtCarrinhoQuantidade = (TextBox)di.FindControl("txtCarrinhoQuantidade");

                    if (txtCarrinhoQuantidade.Text == "0")
                    {
                        string stringconn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        BLL.BD.backoffice oDB = new BLL.BD.backoffice(stringconn);
                        int num = oDB.EliminarCarrinhoNo(Convert.ToInt32(Session["IDCarrinho"]));
                        FormView FormViewQuantidadeCarrinho = Page.Master.FindControl("FormViewQuantidadeCarrinho") as FormView;
                        FormView FormViewTotalCarrinho = Page.Master.FindControl("FormViewTotalCarrinho") as FormView;
                        FormViewQuantidadeCarrinho.DataBind();
                        FormViewTotalCarrinho.DataBind();
                        GridViewManage.DataBind();
                        int rowCount = GridViewManage.Rows.Count;

                        if (rowCount == 0)
                        {
                            Response.Redirect("Produtos.aspx");
                        }
                    }
                    else
                    {

                        string stringconn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        BLL.BD.backoffice oDB = new BLL.BD.backoffice(stringconn);
                        int num = oDB.AlterarQuantidade(Convert.ToInt32(Session["IDCarrinho"]), Convert.ToInt32(txtCarrinhoQuantidade.Text));
                        FormView FormViewQuantidadeCarrinho = Page.Master.FindControl("FormViewQuantidadeCarrinho") as FormView;
                        FormView FormViewTotalCarrinho = Page.Master.FindControl("FormViewTotalCarrinho") as FormView;
                        FormViewQuantidadeCarrinho.DataBind();
                        FormViewTotalCarrinho.DataBind();
                        if (num == 1)
                        {
                            FormView FormViewQuantidadeCarrinho1 = Page.Master.FindControl("FormViewQuantidadeCarrinho") as FormView;
                            FormView FormViewTotalCarrinho1 = Page.Master.FindControl("FormViewTotalCarrinho") as FormView;
                            FormViewQuantidadeCarrinho1.DataBind();
                            FormViewTotalCarrinho1.DataBind();
                            GridViewManage.DataBind();
                            Response.Redirect("Carrinho.aspx");

                        }

                    }
                }
            }

            catch (Exception)
            {

            }
        }

Stored P and BLL

CREATE PROCEDURE [dbo].[AlterarQuantidade]
    @IDCarrinho int,
    @Quantidade int

AS

    UPDATE Carrinho SET Quantidade = @Quantidade WHERE IDCarrinho = @IDCarrinho

RETURN

BLL:

public int AlterarQuantidade(int IDCarrinho, int Quantidade)
            {
                SqlConnection ligacao = getConexao();
                SqlCommand comando = new SqlCommand("AlterarQuantidade", ligacao);
                comando.CommandType = CommandType.StoredProcedure;

                SqlParameter parameteridcarrinho = new SqlParameter("@IDCarrinho", SqlDbType.Int);
                parameteridcarrinho.Value = IDCarrinho;
                comando.Parameters.Add(parameteridcarrinho);

                SqlParameter parameterquant = new SqlParameter("@Quantidade", SqlDbType.Int);
                parameterquant.Value = Quantidade;
                comando.Parameters.Add(parameterquant);


                int num = comando.ExecuteNonQuery();
                closeConexao(ligacao);
                if (num == 1)
                {
                    return 1;

                }
                else
                {
                    return 0;
                }
            }

EDIT : The problem of passing the Cart ID and Product ID is solved, the problem at the moment is the textbox of the quantity in the gridview, when I change the quantity of the 2nd product, for example, that is in the 2nd row, it only considers the quantity of the 1st product, ie it is always going to fetch the quantity of the product from above, and does not consider that of the selected row.

The problem is from here:

TextBox txtCarrinhoQuantidade = (TextBox)di.FindControl("txtCarrinhoQuantidade");

    
asked by anonymous 09.07.2015 / 18:10

1 answer

-1

Should not contain a Where with the product key?

Procedure:

CREATE PROCEDURE [dbo].[AlterarQuantidade] IDCarrinho int, Quantidade int 
    AS UPDATE Carrinho SET Quantidade = 500
    WHERE IDCarrinho = 1 
RETURN

where 1 is the id of an existing cart.

    
09.07.2015 / 20:30