Hello Folks,
I have a shopping cart that is allowing me to add items to the shopping cart that is displayed using a GridView. The GridView has an update link that will allow a user to change the quantity, yet everytime I try to update I get the following error:
"Specified argument was out of the range of valid values. Parameter name:index.
Below is the GridView and the Classes that is generating the error. I have check the text that I am using and it matches the code in the book, yet I can't figure out why the bug is generating.
Regards,
-Tsu
[CART.ASPX.VB]
[SHOPPINGCART.AS PX.VB]
I have a shopping cart that is allowing me to add items to the shopping cart that is displayed using a GridView. The GridView has an update link that will allow a user to change the quantity, yet everytime I try to update I get the following error:
"Specified argument was out of the range of valid values. Parameter name:index.
Below is the GridView and the Classes that is generating the error. I have check the text that I am using and it matches the code in the book, yet I can't figure out why the bug is generating.
Regards,
-Tsu
Code:
<%@ Page Title="ACME Book Supply" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Cart.aspx.vb" Inherits="Cart" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="Your shopping cart is empty." OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"> <Columns> <asp:BoundField DataField="ID" HeaderText="Product" ReadOnly="true"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="true" DataFormatString="{0:c}"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="Quantity" HeaderText="Quantity"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="true" DataFormatString="{0:c}"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:CommandField EditText="Change" ShowDeleteButton="true" ShowEditButton="true"> <ItemStyle BorderStyle="None" /> <HeaderStyle BorderStyle="None" /> </asp:CommandField> </Columns> </asp:GridView> <br /><br /> <asp:Button ID="btnContinue" runat="server" OnClick="btnContinue_Click" Text="Continue Shopping" /> <asp:Button ID="btnCheckOut" runat="server" PostBackUrl="~/CheckOut.aspx" Text="Check Out" /> </asp:Content>
Code:
Protected Sub GridView1_RowUpdating( _ ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _ Handles GridView1.RowUpdating Dim cell As DataControlFieldCell cell = GridView1.Rows(e.RowIndex) _ .Controls(3) Dim t As TextBox = cell.Controls(0) Try Dim q As Integer q = Integer.Parse(t.Text) cart.UpdateQuantity(e.RowIndex, q) Catch ex As FormatException e.Cancel = True End Try GridView1.EditIndex = -1 GridView1.DataBind() End Sub
Code:
Imports Microsoft.VisualBasic Imports System.Collections.Generic Public Class ShoppingCart Private _cart As List(Of CartItem) Public Sub New() _cart = New List(Of CartItem)() End Sub Public Function GetItems() As List(Of CartItem) Return _cart End Function Public Sub AddItem(ByVal id As String, ByVal name As String, ByVal price As Decimal) Dim itemFound As Boolean = False For Each item As CartItem In _cart If item.ID = id Then item.Quantity += 1 itemFound = True End If Next If Not itemFound Then Dim item As CartItem item = New CartItem(id, name, price, 1) _cart.Add(item) End If End Sub Public Sub UpdateQuantity(ByVal Index As Integer, ByVal quantity As Integer) Dim item As CartItem item = _cart(Index) item.Quantity = quantity End Sub Public Sub DeleteItem(ByVal index As Integer) _cart.RemoveAt(index) End Sub Public ReadOnly Property Count() As Integer Get Return _cart.Count End Get End Property End Class
Comment