Loading ...

How to add image and other fields to show in Products & ShoppingCart

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  web forms / data controls   » How to add image and other fields to show in Products & ShoppingCart

How to add image and other fields to show in Products & ShoppingCart

Posts under the topic: How to add image and other fields to show in Products & ShoppingCart

Posted: 10/10/2011

Lurker 5  points  Lurker
  • Joined on: 10/10/2011
  • Posts: 1
Made project in VS2008. The good news, it runs and build right. Bad news is that it only shows the Product name in Products.aspx and not the other fields. Plus also bad news that it does not show nothing in the ShoppingCart.aspx. I want everything to show in the ShoppingCart.aspx including  the ProdName,Quantity, Description and Image.
I am using a Repeater in both pages and want all the fields except ProductID to show in both pages.
I have attached the Project with this forum.

------------------------------------------
******ShoppingCart.aspx code:
---------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShoppingCart.aspx.cs" Inherits="SampleShopThis.ShoppingCart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1" width="80%" cellpadding = "0" cellspacing="0" >
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</td>
</tr>
<asp:Repeater ID="rptShoppingCart" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"Counter") %>
.<%# DataBinder.Eval(Container.DataItem,"ProductName") %>
</td>
<td>
<a href="ShoppingCart.aspx?action=remove&id=<%# DataBinder.Eval(Container.DataItem,"ProductID") %>">
Remove</a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<a href='Product.aspx'>Continue Shopping</a>
<br />
<a href=''>Check Out </a>
    </div>
    </form>
</body>
</html>
---------------------------------------------------------
******ShoppingCart.aspx.cs code:
-------------------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace SampleShopThis
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private void AddToShoppingCart(int ProductID)
        {
            if (Request.Cookies["ShoppingCart.aspx"] == null)
            {
                HttpCookie oCookie = new HttpCookie("ShoppingCart.aspx");
                //Set Cookie to expire in 3 hours
                oCookie.Expires = DateTime.Now.AddHours(3);
                oCookie.Value = ProductID.ToString();
                Response.Cookies.Add(oCookie);
            }
            else
            {
                bool bExists = false; char[] sep = { ',' };
                HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart.aspx"];
                //Set Cookie to expire in 3 hours
                oCookie.Expires = DateTime.Now.AddHours(3);
                //Check if Cookie already contain same item
                string sProdID = oCookie.Value.ToString();
                string[] arrCookie = sProdID.Split(sep);
                for (int i = 0; i < arrCookie.Length; i++)
                {
                    if (arrCookie[i].Trim() == ProductID.ToString().Trim())
                    {
                        bExists = true;
                    }
                }
                if (!bExists)
                {
                    if (oCookie.Value.Length == 0)
                    {
                        oCookie.Value = ProductID.ToString();
                    }
                    else
                    {
                        oCookie.Value = oCookie.Value + "," + ProductID;
                    }
                }
                //Add back into  the Response Objects.
                Response.Cookies.Add(oCookie);
            }
            if (Request.Cookies["ShoppingCart.aspx"] == null)
            {
                //Do nothing
            }
            else
            {
                HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart.aspx"];
                //Set Cookie to expire in 3 hours
                char[] sep = { ',' };
                oCookie.Expires = DateTime.Now.AddHours(3);
                //Check if Cookie already contain same item
               string  sProdID = oCookie.Value.ToString();
                string[] arrCookie = sProdID.Split(sep);
                string[] arrCookie2 = new string[arrCookie.Length - 1];
                int j = 0; for (int i = 0; i < arrCookie.Length; i++)
                {
                    if (arrCookie[i].Trim() != ProductID.ToString())
                    {
                        arrCookie2[j] = arrCookie[i];
                        j++;
                    }
                }
                string sCookieID = ""; for (int i = 0; i < arrCookie2.Length; i++)
                {
                    sCookieID = sCookieID + arrCookie2[i] + ",";
                }
                if (sCookieID.Length > 0)
                {
                    oCookie.Value = sCookieID.Substring(0, sCookieID.Length - 1);
                }
                else
                {
                    oCookie.Value = "";
                }
                //Add back into  the Response Objects.
                Response.Cookies.Add(oCookie);
            }
        }
    }
}
---------------------------------------------------
Product.aspx
---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Product.aspx.cs" Inherits="SampleShopThis._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>My E-Commerce Shop</h2>
<asp:Repeater ID="rptProducts" RUNAT="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"ProdName") %>
</td>
<td><a href='ShoppingCart.aspx?action=add&ID=<%#DataBinder.Eval(Container.DataItem,"ProdName") %>'>
Add to Shopping Cart</a></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnString %>" 
                SelectCommand="SELECT [ProdName], [Description], [Image] FROM [Products]">
            </asp:SqlDataSource>
<br />
<a href="ShoppingCart.aspx">My Shopping Cart</a>
<br />
<a href="Checkout.aspx">CheckOut</a>
    </div>
    </form>
</body>
</html>
------------------------------------------------------------
Products.aspx.cs
------------------------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace SampleShopThis
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select ProdName,Description,Quantity, Image from Products";//where supplierid = 2";
            SqlDataReader dr = cmd.ExecuteReader();
            //rptProducts.DataSource = dr;
            //rptProducts.DataBind();
            dr.Close();
            conn.Close();
        }
    }
}
--------------------------------------------------------------

Posted: 10/10/2011

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

Hi,

In order to get other fields on Product.aspx you need to write your ItemTemplate like this:

<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"ProdName") %>
</td>
<td><%#DataBinder.Eval(Container.DataItem, "Description")%>
</td>
<td><%#DataBinder.Eval(Container.DataItem, "Image")%>
</td>
<td><a href='ShoppingCart.aspx?action=add&ID=<%#DataBinder.Eval(Container.DataItem,"ProdName") %>'>
Add to Shopping Cart</a></td>
</tr>
</ItemTemplate>

 and in ShoppingCart.aspx you are getting ID as the querystring in the URL so use it to fetch the product and show in repeater.


Page 1 of 1 (2 items)