Click here to Skip to main content
15,116,354 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I would appreciate some help for my below issue:
I have a site that sells games etc, each game has an add to cart link on its individual page, clicking that brings you to "confirmamount.aspx" which asks how many they wish to purchase, typing a figure then goes to a shopping cart with a gridview containing the product id, product name, quantity user requested to buy, price per unit, and then "total price", all of this i have working but i want in the footer of my gridview to say the totalbill for all items prices, so far i have below code but the footer just remains blank.

CONFIRMAMOUNT.ASPX.CS

C#
public partial class ConfirmAmount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int ProductID = int.Parse(Request["id"].ToString());
            SqlCommand oCMD = new SqlCommand();
            SqlConnection oCON = new SqlConnection();
            oCON.ConnectionString = ConfigurationManager.ConnectionStrings["ProductsConnectionString"].ConnectionString;
            oCON.Open();
            oCMD.Connection = oCON;
            oCMD.CommandType = CommandType.StoredProcedure;
            oCMD.CommandText = "GetProductNameForID";
            oCMD.Parameters.Add(new SqlParameter("@ProdID", SqlDbType.Int));
            oCMD.Parameters["@ProdID"].Value = ProductID;
            SqlDataReader oRDR = oCMD.ExecuteReader();
            oRDR.Read();
            lblProduct.Text = oRDR.GetString(oRDR.GetOrdinal("title"));
            lblPrice.Text = oRDR.GetString(oRDR.GetOrdinal("price"));
            oCON.Close();
        }
        protected void cmdConfirm_Click(object sender, EventArgs e)
        {
            ((ArrayList)Session["aList"]).Add(new CustomerOrder(int.Parse(Request["id"]), lblProduct.Text, int.Parse(txtQuantity.Text), lblPrice.Text));
            Server.Transfer("ShoppingCart.aspx");
        }
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            cmdCancel.Attributes.Add("onClick", "javascript:history.back(); return false;");
        }
    }

CONFIRMAMOUNT.ASPX
ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="ConfirmAmount.aspx.cs" Inherits="ConfirmAmount" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
         <div>
            <table>
                <tr>
                    <td style="width: 700px">
                        <h2 align="center">
                            style="color: #0441ff; font-family: 'Corbel';"&gt;Confirm purchase quantity:</h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        Adding 
                        <asp:Label ID="lblProduct" runat="server" Text="Label">
                          to your cart at €
                        <asp:Label ID="lblPrice" runat="server" Text="Label">
                    </td>
                </tr>
                <tr>
                    <td>
                        Please enter your quantity: 
                        <asp:TextBox ID="txtQuantity" runat="server">
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="cmdConfirm" runat="server" Text="Confirm" 
                            onclick="cmdConfirm_Click"/>
                        <asp:Button ID="cmdCancel" runat="server" Text="Cancel" 
                            onclick="cmdCancel_Click" onclientclick="javascript:history.back(); return false" />
                    </td>
                </tr>
            </table>
        </div>

CUSTOMER.CS

C#
using System;
    using System.Collections.Generic;
    //using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for CustomerOrder
    /// </summary>
    public class CustomerOrder
    {
        private int iProductID;
        private string sProductName;
        private int iQuality;
        private string iPrice;
    
        public CustomerOrder(int PID, string PN, int Q, string Pri)
        {
            this.iProductID = PID;
            this.sProductName = PN;
            this.iQuality = Q;
            this.iPrice = Pri;
        }
    
        public int ProductID
        {
            get
            {
                return iProductID;
            }
            set
            {
                iProductID = value;
            }
        }
    
        public string Productname
        {
            get
            {
                return sProductName;
            }
            set
            {
                sProductName = value;
            }
        }
    
        public int Quantity
        {
            get
            {
                return iQuality;
            }
            set
            {
                iQuality = value;
            }
        }
    
        public string ProductPrice
        {
            get
            {
                return iPrice;
            }
            set
            {
                iPrice = value;
            }
        }
        //public CustomerOrder()
        //{
        //    //
        //    // TODO: Add constructor logic here
        //    //
        //}
    }

SHOPPINGCART.ASPX
ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="ShoppingCart" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
        <div>
            <table>
                <tr>
                    <td style="width: 500px">
                        <h2 align="center">
                            style="color: #0441ff; font-family: 'Corbel';"&gt;Shopping Cart:</h2>
                    </td>
                </tr>
                <tr>
                    <td>
    &lt;asp:GridView ID="grdProducts" runat="server" AutoGenerateColumns="False" 
                            HorizontalAlign="Center" Width="550px" ShowFooter="True" BorderStyle="None" GridLines="None"&gt;
                            <columns>
                                <asp:BoundField DataField="ProductName" HeaderText="Product Name" 
                                    ItemStyle-HorizontalAlign="Center" >
                                    <itemstyle horizontalalign="Center"></itemstyle>
                                
                                <asp:BoundField DataField="ProductID" HeaderText="Product ID" 
                                    ItemStyle-HorizontalAlign="Center" >
                                    <itemstyle horizontalalign="Center"></itemstyle>
                                
                                &lt;asp:BoundField DataField="ProductPrice" HeaderText="Price" 
                                    ItemStyle-HorizontalAlign="Center" DataFormatString="€{0:d}" &gt;
                                    <itemstyle horizontalalign="Center"></itemstyle>
                                
                                &lt;asp:TemplateField HeaderText="Quantity"&gt;
                                    <itemtemplate>
                                        &lt;asp:Label ID="lblQuantity" runat="server" BorderColor="Black" Text='&lt;%# Eval("quantity")%&gt;'/&gt;
                                    </itemtemplate>
                                    <footertemplate>
                                        &lt;asp:Label ID="lblBuying" runat="server" BorderColor="Black" /&gt;
                                    </footertemplate>
                                    <footerstyle borderstyle="Solid" />
                                    <itemstyle horizontalalign="Center" />
                                
                                <asp:TemplateField HeaderText="Total Price" ItemStyle-HorizontalAlign="Center" FooterStyle-BorderStyle="Solid">
                                    <itemtemplate>
                                        <asp:Label ID="lblQuPrice" runat="server" BorderColor="Black" Text ='&lt;%# "€" + (Decimal.Parse(Eval("ProductPrice").ToString())*Decimal.Parse(Eval("Quantity").ToString())).ToString("N2") %&gt;'>
                                        
                                     </itemtemplate>
                                    <footertemplate>
                                        <asp:Label ID="lblBill" runat="server" BorderColor="Black" />
                                    </footertemplate>
                                    <footerstyle borderstyle="Solid"></footerstyle>
                                    <itemstyle horizontalalign="Center"></itemstyle>
                                
                            </columns>
                            <editrowstyle horizontalalign="Center" />
                            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" 
                                ForeColor="#003296" />
                            <footerstyle horizontalalign="Right" />
                        
                        <asp:SqlDataSource ID="ShopCartDataSource" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>" 
                            SelectCommand="SELECT [id], [title], [quantity], [price] FROM [productTable]">
                        
                    </td>
                </tr>
                <tr align="center">
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Buy Now"/>
                        <asp:Button ID="Button2" runat="server" Text="Continue Shopping" 
                            PostBackUrl="~/HomePage.aspx" />
                    </td>
                </tr>
            </table>
        </div>


SHOPPINGCART.ASPX.CS
C#
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList aList;
        aList = (ArrayList)Session["aList"];
        grdProducts.DataSource = aList;
        grdProducts.DataBind();
    }

    decimal theBill = 0;
    decimal totalStock = 0;

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblQuPrice = (Label)e.Row.FindControl("lblQuPrice");
            Label lblQuantity = (Label)e.Row.FindControl("lblQuantity");

            decimal price = Decimal.Parse(lblQuPrice.Text);
            decimal amountToBuy = Decimal.Parse(lblQuantity.Text);

            theBill += price;
            totalStock += amountToBuy;
        }
        if(e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblBill = (Label)e.Row.FindControl("lblBill");
            Label lblBuying = (Label)e.Row.FindControl("lblBuying");

            lblBill.Text = theBill.ToString();
            lblBuying.Text = totalStock.ToString();
        }
    }
}
Posted
Updated 16-Jun-12 7:52am
v2

1 solution

I doubt if anyone is going to go throw that whole code dump. You should always share the related code snippet to the issue related.

For now, have a look at this MSDN article on the same topic to see how it is implemented: MSDN: GridView Examples for ASP.NET 2.0: Displaying Summary Data in the Footer[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900