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);
}'