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);
}`
@There’s only one catch, the status of the order is always "Processing"...
– Chirag Geiantilal
The
int encomenda
needs theestado
, I had to put it in, so it’s always "processing".int idencomenda = oDB.InserirEncomenda(Convert.ToInt32(Session["Iduser"]), estado);
– Chirag Geiantilal