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.
– Joel Rodrigues
See the answer edited above.
– Chirag Geiantilal
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?– Chirag Geiantilal
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.
– Chirag Geiantilal
Can someone help me?
– Chirag Geiantilal