Problem doing refresh of quantity shopping cart C#

Asked

Viewed 204 times

0

By refreshing 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’t refresh one of them.

Imagine that the 1st 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 it is not possible, it is only possible to 5 units, that is to say if you want to change the quantity of a product, all products have to have the same quantity number. I tried several ways but I could not solve 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 ID Cart and ID product is solved, the problem at the moment is the textbox of the quantity in gridview, when I change the quantity of the 2nd product for example, which is in the 2nd Row, it only considers the quantity of the 1st product, that is to say it is always going to fetch the quantity of the product from above, and does not consider that from Selected Row.

The problem is from here:

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

  • How is your UPDATE statement in the table? By its description, it appears that the WHERE clause is missing, or is incorrectly defined.

  • See the answer edited above.

  • I already know the problem, the problem is that I am not passing the Product ID when I click on the Image Button to update the Cart. How can I pass the Product ID from Selected Row? I have tried it in several ways but without success. I am sending the Cart ID this way: protected void imgBtnUpdateCart_Click(object sender, EventArgs e)
 {
 ImageButton lnksender = (ImageButton)sender;
 Session["IDCarrinho"] = Convert.ToInt32(lnksender.CommandArgument); How can I send the cart ID and the Product ID from Selected Row at the same time?

  • The problem is really the textbox that is in gridview where the quantity of the product is inserted, the textbox considered is always the 1st, so the quantity value of the remaining products is always equal to the 1st. How can I check every Row the value of the textbox? The textbox is inside templatefield.

  • Can someone help me?

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 respective id of an existing cart.

  • When I put & #Xa; UPDATE Carrinho SET Quantidade = @Quantidade WHERE IDCarrinho = @IDCarrinho AND IDProduto = @IDProduto .

  • Try going straight to the bank, see if it rolls.

  • Yes that way it goes.

  • Changed in the Procedure?

  • Only in the past? .

  • Put the value right on Procedure, just to see if it’s gonna rock

  • I didn’t realize, could exemplify?

  • CREATE PROCEDURE [dbo]. [Change quantity] Idcarrinho int, Quantity int AS UPDATE Cart SET Quantity = 500 WHERE Idcarrinho = 1 RETURN where 1 is the respective id of an existing cart

  • Yes it worked out that way.

  • @Chiraggeiantilal your problem is solved with the trial proposed by Guilherme?

  • Can’t be solved.

  • @Chiraggeiantilal, your problem clearly lies in the WHERE, as has already been said. You need to filter beyond the cart ID, also the product ID that is being changed. Work on this idea.

  • This has already been done, the problem is that when I change the amount of the textbox in gridview is only considered the first textbox, the problem is there. The rest is done, you are already considering the Cart ID and Product ID.

  • @Joelrodrigues updated the question with more details of the current error.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.