<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="shoppingCart1.aspx.cs" Inherits="shoppingCart1" Title="Untitled Page" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" OnRowCommand="updateCart" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Table ID="Table1" runat="server" Width="900px">
<asp:TableRow><asp:TableCell Width="400px">
<asp:Image ID="Image3" Height = "300px" Width = "300px" runat="server" ImageUrl = '<%# Bind("img_category", "imageRetrieve.aspx?ImageID={0}")%>' />
</asp:TableCell>
<asp:TableCell>Product Name: <%# Eval("img_name")%><br />
Product Category: <%# Eval("img_category")%><br />
Product Quantity: <%# Eval("img_quantity")%><br />
Product Price: <%# Eval("img_price")%><br />
<asp:Button ID="Button1" runat="server" Text="Add To Cart" CommandName="AddToCart" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="shopCart" runat="server">
</asp:GridView>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class shoppingCart1 : System.Web.UI.Page
{
DataTable Cart;
DataView CartView;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetInventory();
}
if (Session["shoppingSession"] == null)
{
Cart = new DataTable();
Cart.Columns.Add(new DataColumn("img_name", typeof(string)));
Cart.Columns.Add(new DataColumn("img_category", typeof(string)));
Cart.Columns.Add(new DataColumn("img_quantity", typeof(string)));
Cart.Columns.Add(new DataColumn("img_price", typeof(string)));
Session["shoppingSession"] = Cart;
}
else Cart = (DataTable)Session["shoppingSession"];
CartView = new DataView(Cart);
shopCart.DataSource = CartView;
shopCart.DataBind();
}
protected void GetInventory()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["JSLighting"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Image", con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
adapter.SelectCommand = cmd;
adapter.Fill(data);
GridView1.DataSource = data;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
adapter.Dispose();
con.Dispose();
data.Dispose();
}
}
protected void updateCart(Object src, GridViewCommandEventArgs e)
{
DataRow dr = Cart.NewRow();
// get the row index stored in the CommandArgument property
// error occur here
int index = Convert.ToInt32(e.CommandArgument);
// get the GridViewRow where the command is raised
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
// for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
string name = selectedRow.Cells[1].Text;
string category = selectedRow.Cells[2].Text;
string quantity = selectedRow.Cells[3].Text;
string price = selectedRow.Cells[4].Text;
if (e.CommandName == "AddToCart")
{
dr[0] = name;
dr[1] = category;
dr[2] = quantity;
dr[3] = price;
Cart.Rows.Add(dr);
}
else
{
// remove from Cart
CartView.RowFilter = "img_name='" + name + "'";
if (CartView.Count > 0) CartView.Delete(0);
CartView.RowFilter = "";
}
shopCart.DataBind();
}
}
!--removed tag-->
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).