I'm trying to create a homepage for my asp .net project, which should display all the products available from the database. The code below is my aspx code, and I've labelled the code I'm trying to loop and display all the results from the database with --> and <--.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="Try.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 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">
<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(*product image*) %>" alt="..." />
<!-- Product details-->
<div class="card-body p-4">
<div class="text-center">
<!-- Product name-->
<h5 class="fw-bolder">*product name*</h5>
<!-- Product price-->
*product price*<br/>
*product description*
</div>
</div> <--
<!-- Product actions-->
<div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
<div class="text-center"><a class="btn btn-outline-dark mt-auto" href="productDetails.aspx">Details</a></div><br />
<div class="text-center"><a class="btn btn-outline-dark mt-auto" href="home.aspx">Add to cart</a></div>
</div>
</div>
</div>
</div>
</div>
</section>
</asp:Content>
And the code below are the code which I currently left at the aspx.cs page to run the SQL and retrieve the result. I wish to put it somewhere within the aspx page with <% %> but there are errors.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Try
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strHome = "SELECT * FROM PRODUCT";
SqlCommand cmdHome = new SqlCommand(strHome, con);
SqlDataReader dtrHome = cmdHome.ExecuteReader();
while (dtrHome.Read())
{
//display the result from database on the aspx page
}
}
}
}





