Here is my ASPX markup to display all the products available from the database, and allow the user to view details and add to cart:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="WADAssignment.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form runat="server" >
<section class="py-5">
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
<asp:Repeater ID="RepeaterHome" runat="server">
<ItemTemplate>
<div class="col mb-5">
<div class="card h-100">
<!-- Product image-->
<img class="card-img-top" width="205.99px" height="205.99px" src="data:image/jpg;base64,<%# @Convert.ToBase64String((byte[])(Eval("prod_photo"))) %>" alt="..." />
<!-- Product details-->
<div class="card-body p-4">
<div class="text-center">
<!-- Product name-->
<asp:HiddenField ID="prodId" Value='<%# Eval("prod_id") %>' runat="server" />
<h5 class="fw-bolder"><%# Eval("prod_name") %></h5>
<!-- Product price-->
RM <%# Eval("prod_price") %><br/>
<%# Eval("prod_desc") %>
</div>
</div>
<!-- Product actions-->
<div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
<div class="text-center">
<asp:Button CssClass="btn btn-outline-dark mt-auto" ID="Button2" runat="server" Text="View Details" PostBackUrl='<%# "productDetails.aspx?id=" + Eval("prod_id") %>'/>
</div><br />
<div class="text-center">
<asp:Button CssClass="btn btn-outline-dark mt-auto" ID="Button1" runat="server" Text='<%# Convert.ToInt32(Eval("prod_quantity")) > 0 ? "Add to Cart" : "Sold out" %>' Enabled='<%# Convert.ToInt32(Eval("prod_quantity")) > 0 ? true : false %>' OnClick="Button1_Click" />
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</section>
</form>
</asp:Content>
This is the onclick function for the add to cart button, however with my current implementation of going through the repeater item list, the biggest prodId will always be selected, instead of the respective one, and then causing a SQL error.
protected void Button1_Click(object sender, EventArgs e)
{
HiddenField hidden = null;
foreach (RepeaterItem item in RepeaterHome.Items)
{
hidden = (HiddenField)item.FindControl("prodId");
}
int prodId = Convert.ToInt32(hidden.Value);
String id = Session["id"].ToString();
string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strCon))
{
string strCart = "INSERT INTO CART_ITEM (id, prod_id, quantity) values (@id, @prod_id, @quantity)";
string strUpdateProd = "UPDATE PRODUCT SET PROD_QUANTITY = PROD_QUANTITY - 1 WHERE PROD_ID = @prod_id";
using (SqlCommand cmdCart = new SqlCommand(strCart, con))
{
try
{
con.Open();
cmdCart.Parameters.AddWithValue("@id", id);
cmdCart.Parameters.AddWithValue("@prod_id", prodId);
cmdCart.Parameters.AddWithValue("@quantity", 1);
SqlDataReader dtrCart = cmdCart.ExecuteReader();
dtrCart.Close();
con.Close();
using (SqlCommand cmdUpdateProd = new SqlCommand(strUpdateProd, con))
{
con.Open();
cmdUpdateProd.Parameters.AddWithValue("@prod_id", prodId);
SqlDataReader dtrUpdateProd = cmdUpdateProd.ExecuteReader();
dtrUpdateProd.Close();
con.Close();
}
} catch (SqlException)
{
Response.Write("<script>alert('Item already in cart!')</script>");
}
}
}
}
This screenshot is showing my web page:
Ok, you want the current item you click on. But you ahve a loop (for each) like this:
the above will loop for each item, and then when done it will ALWAYS be the last item.
You want to JUST get the current ONE item that you clicked on, not loop (for each) as you have above.
So, your code can be like this (example code).
Now, above will do the update and add to the cart. But, we have good chance that we want to check if the item is already in the cart, and not allow to add again. (or just increase the qty if they choose again - not sure which choice you want). So, the above will allow adding again and again. So, before we run the above? We should find out if the item already in the cart. (that part is missing, and was not part of your question).