anyone can help me to fix the index error?

1 , 2 , 3 , 4 , 5 Kuala Lumpur, Malaysia
  • 8 years ago

    <%@ 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" >
            <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>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:GridView ID="shopCart" runat="server">

    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)
        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;
    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;
            adapter.SelectCommand = cmd;
            GridView1.DataSource = data;
        catch (Exception ex)
    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; 
            // remove from Cart
            CartView.RowFilter = "img_name='" + name + "'";
            if (CartView.Count > 0) CartView.Delete(0);
            CartView.RowFilter = "";


Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond