- I am saving the products in the tbl_ShopCart to the tbl_Order
- There can be more than one product because I use repeater for shop cart.
- For example: My shop cart have 2 products and I push button to complete shopping and I have saved the data for the tbl_Order. But the total price is recorded separately for the two products. And I want to show the total price in one line for two products.
And my aspx.cs code:
protected void BtnCompleteOrder_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in Repeater1.Items)
{
if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Label lblproductid = (Label)item.FindControl("LblProductID");
Image imgproductimage = (Image)item.FindControl("ImgProductImage");
Label lblproductname = (Label)item.FindControl("LblProductName");
Label lblprice = (Label)item.FindControl("LblPrice");
Label lblpiece = (Label)item.FindControl("LblPiece");
function.cmd("INSERT INTO tbl_Order(userid, productid, name, surname, email, identificationnumber, phone, productimage, productname, piece, cargo, totalprice, paymenttype, orderdate) VALUES('" + Session["userid"] + "', '" + lblproductid.Text + "', '" + Session["name"] + "', '" + Session["surname"] + "', '" + Session["email"] + "', '" + Session["identificationnumber"] + "', '" + Session["phone"] + "', '" + imgproductimage.ImageUrl + "', '" + lblproductname.Text + "', '" + lblpiece.Text + "', '" + Session["cargo"] + "', '" + LblTotalPrice.Text + "', '" + DrpDwnPaymentType.Text + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')");
}
}
}
// I add the cargo price on top of the total price, which gives us the lasttotal.
decimal total = 0;
decimal lasttotal = 0;
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView item = e.Item.DataItem as DataRowView;
total += Convert.ToDecimal(item["price"]) * Convert.ToDecimal(item["piece"]);
LblShopCartTotal.Text = total.ToString();
lasttotal = total + Convert.ToDecimal(LblCargo.Text);
LblTotalPrice.Text = lasttotal.ToString();
}
This is my design code and design window:
<table class="table shop-cart text-center">
<thead>
<tr>
<th class="first"></th>
<th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Product Name</th>
<th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Price</th>
<th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Piece</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<asp:Label ID="LblShopCartID" runat="server" Text='<%#Eval("shopcartid") %>' Visible="false"></asp:Label>
<asp:Label ID="LblProductID" runat="server" Text='<%#Eval("productid") %>' Visible="false"></asp:Label>
<td class="product-thumbnail text-left"><asp:Image ID="ImgProductImage" runat="server" Height="150px" Width="150px" ImageUrl='<%#Eval("productimage") %>' /></td>
<td class="text-left"><asp:Label ID="LblProductName" runat="server" Text='<%#Eval("productname") %>'></asp:Label></td>
<td class="text-left"><asp:Label ID="LblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> TL</td>
<td class="product-subtotal text-left"><asp:Label ID="LblPiece" runat="server" Text='<%#Eval("piece") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT * FROM [tbl_ShopCart] WHERE ([userid] = @userid)">
<SelectParameters>
<asp:SessionParameter Name="userid" SessionField="userid" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</tbody>
</table>
<table class="table cart-total">
<tbody>
<tr>
<th class="padding-two text-right no-padding-right text-uppercase font-weight-600 letter-spacing-2 text-small xs-no-padding">Shop Cart Total: </th>
<td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text xs-no-padding"><asp:Label ID="LblShopCartTotal" Text="" runat="server"></asp:Label> ₺</td>
</tr>
<tr>
<th class="padding-two text-right no-padding-right text-uppercase font-weight-600 letter-spacing-2 text-small xs-no-padding">Cargo Price: </th>
<td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text text-small xs-no-padding"><asp:Label ID="LblCargoPrice" runat="server" Text=""></asp:Label> ₺</td>
</tr>
<tr class="total">
<th class="padding-two text-uppercase text-right no-padding-right font-weight-600 text-large xs-no-padding">Last Total: </th>
<td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text text-large no-letter-spacing xs-no-padding"><asp:Label ID="LblTotalPrice" runat="server" Text=""></asp:Label> ₺</td>
</tr>
</tbody>
</table>