Problem with foreach Shopping Cart

Asked

Viewed 84 times

0

I’m having a problem with foreach and I wonder if there’s another method to not have to change all the code.

What happens is that each time more than one product is added to the cart, if we buy for example 2 products instead of being all in the same order, each item is placed in separate orders, summarizing each item has 1 order ID and not all in the same order.

In other words, what I want is that when an order is placed with more than 1 product, and of course all the items are placed in the same order.

And this is due to foreach, there’s some way around this?

I leave the code here:

try
{
    foreach (GridViewRow di in GridView1.Rows)
    {
        TextBox txtQuantcarrinho = (TextBox)di.FindControl("txtQuantcarrinho");
        int quantstock = oDB.ReceberQuantidade(Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text));
        if (Convert.ToInt32(txtQuantcarrinho.Text) > quantstock)
        {
            int idencomenda = oDB.InserirEncomenda(Convert.ToInt32(Session["Iduser"]), "A aguardar produtos‏");
            oDB.InserirProdutoEncomenda(Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text), idencomenda, Convert.ToInt32(txtQuantcarrinho.Text));
            int num = oDB.ApagarCarrinho(Convert.ToInt32(Session["Iduser"]), Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text));
        }
        else 
        {
            int idencomenda = oDB.InserirEncomenda(Convert.ToInt32(Session["Iduser"]), "A processar");
            oDB.InserirProdutoEncomenda(Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text), idencomenda, Convert.ToInt32(txtQuantcarrinho.Text));
            int num = oDB.ApagarCarrinho(Convert.ToInt32(Session["Iduser"]), Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text));
        }

    }

    foreach (GridViewRow di in GridView1.Rows)
    {


    }
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}`

1 answer

2


I think the problem is that you are inserting the order within the foreach, but actually you should do it outside of it and inside it you add only the order items.

string estado = "A processar";

int idencomenda = oDB.InserirEncomenda(Convert.ToInt32(Session["Iduser"]));

foreach (GridViewRow di in GridView1.Rows)
{
    oDB.InserirProdutoEncomenda(Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text), idencomenda, Convert.ToInt32(txtQuantcarrinho.Text));
    int num = oDB.ApagarCarrinho(Convert.ToInt32(Session["Iduser"]), Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text));


    // verificar o estado conforme a quantidade em estoque
    TextBox txtQuantcarrinho = (TextBox)di.FindControl("txtQuantcarrinho");
    int quantstock = oDB.ReceberQuantidade(Convert.ToInt32(GridView1.Rows[di.RowIndex].Cells[0].Text));
    if (Convert.ToInt32(txtQuantcarrinho.Text) > quantstock)
    {           
        estado = "A aguardar produtos";
    }
}

After doing this, you must change the order again and set the estado obtained within the foreach. Or, indicate this state in each product of the order.

  • @There’s only one catch, the status of the order is always "Processing"...

  • The int encomenda needs the estado, I had to put it in, so it’s always "processing". int idencomenda = oDB.InserirEncomenda(Convert.ToInt32(Session["Iduser"]), estado);

Browser other questions tagged

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