Asp.net 2.0 update problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tsubasa
    New Member
    • Aug 2008
    • 64

    Asp.net 2.0 update problem

    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

    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" />&nbsp;
        <asp:Button ID="btnCheckOut" runat="server"
        PostBackUrl="~/CheckOut.aspx"
        Text="Check Out" />
    </asp:Content>
    [CART.ASPX.VB]
    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
    [SHOPPINGCART.AS PX.VB]
    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
    Last edited by Frinavale; Oct 28 '13, 05:55 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Consider using the FindControl method to retrieve your TextBox from the GridViewRow.

    For example:
    Code:
    TextBox  t = (TextBox)GridView1.Rows(e.Row.RowIndex).FindControl("TextBox1"));
    You may not even have to go through the GridView1.Rows parameter. You could probably access the row using the sender parameter of the RowUpdating event:
    Code:
    TextBox  t = (TextBox)((GridViewRow) sender).FindControl("TextBox1"));
    -Frinny
    Last edited by Frinavale; Oct 29 '13, 01:07 PM.

    Comment

    • tsubasa
      New Member
      • Aug 2008
      • 64

      #3
      I will give this a try. Thanks for the technical advise.

      -Tsu

      Comment

      • tsubasa
        New Member
        • Aug 2008
        • 64

        #4
        Hello Frinny,

        I think you wrote your code in C#, my code is in VB can you please provide the VB code. Thanks!

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Try using tools like this online converter tool in the future. You will find that most code examples are in C#.

          You can use DirectCast or CType to cast the objects into the types you expect.

          Code:
          Dim t As TextBox = DirectCast( DirectCast( sender,GridViewRow).FindControl("TextBox1"), TextBox)

          Code:
          Dim t As TextBox = CType( CType( sender,GridViewRow).FindControl("TextBox1"), TextBox)

          -Frinny

          Comment

          Working...