Problem with foreach Shopping Cart

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 2 products instead of getting everything in the same order, each item is placed in separate packages, each item is left with 1 ID and everything is not in the same order.

Or what I want is that when an order is made with more than 1 product, and of course all the articles stay in the same order.

And this is due to foreach , is there any 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);
}'
    
asked by anonymous 09.07.2015 / 13:51

1 answer

2

I think the problem is that you are inserting the order within foreach , but in fact you should make it out of it and inside it you only add 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 you do this, you must change the order again and set the estado obtained within foreach . Or, indicate this status in each product of the order.

    
09.07.2015 / 17:23