By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

Asp.net 2.0 update problem

tsubasa
P: 64
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

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Title="ACME Book Supply" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Cart.aspx.vb" Inherits="Cart" %>
  2.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  6. <br />
  7. <asp:GridView ID="GridView1" runat="server" 
  8.     AutoGenerateColumns="false"
  9.     EmptyDataText="Your shopping cart is empty."
  10.     OnRowDeleting="GridView1_RowDeleting"
  11.     OnRowEditing="GridView1_RowEditing"
  12.     OnRowUpdating="GridView1_RowUpdating"
  13.     OnRowCancelingEdit="GridView1_RowCancelingEdit">
  14.     <Columns>
  15.         <asp:BoundField DataField="ID"
  16.             HeaderText="Product" ReadOnly="true">
  17.             <HeaderStyle HorizontalAlign="Left" />
  18.         </asp:BoundField>
  19.          <asp:BoundField DataField="Name"
  20.             HeaderText="Name" ReadOnly="true">
  21.             <HeaderStyle HorizontalAlign="Left" />
  22.         </asp:BoundField>
  23.          <asp:BoundField DataField="Price"
  24.             HeaderText="Price" ReadOnly="true"
  25.             DataFormatString="{0:c}">
  26.             <HeaderStyle HorizontalAlign="Left" />
  27.         </asp:BoundField>
  28.         <asp:BoundField DataField="Quantity"
  29.             HeaderText="Quantity">
  30.             <HeaderStyle HorizontalAlign="Left" />
  31.         </asp:BoundField>
  32.         <asp:BoundField DataField="Total"
  33.             HeaderText="Total" ReadOnly="true"
  34.             DataFormatString="{0:c}">
  35.             <HeaderStyle HorizontalAlign="Left" />
  36.         </asp:BoundField>
  37.         <asp:CommandField EditText="Change"
  38.             ShowDeleteButton="true"
  39.             ShowEditButton="true">
  40.             <ItemStyle BorderStyle="None" />
  41.             <HeaderStyle BorderStyle="None" />
  42.         </asp:CommandField>
  43.     </Columns>
  44.     </asp:GridView>
  45.     <br /><br />
  46.     <asp:Button ID="btnContinue" runat="server"
  47.         OnClick="btnContinue_Click"
  48.         Text="Continue Shopping" />&nbsp;
  49.     <asp:Button ID="btnCheckOut" runat="server"
  50.     PostBackUrl="~/CheckOut.aspx"
  51.     Text="Check Out" />
  52. </asp:Content>
[CART.ASPX.VB]
Expand|Select|Wrap|Line Numbers
  1. Protected Sub GridView1_RowUpdating( _
  2.             ByVal sender As Object, _
  3.             ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
  4.         Handles GridView1.RowUpdating
  5.         Dim cell As DataControlFieldCell
  6.         cell = GridView1.Rows(e.RowIndex) _
  7.             .Controls(3)
  8.         Dim t As TextBox = cell.Controls(0)
  9.         Try
  10.             Dim q As Integer
  11.             q = Integer.Parse(t.Text)
  12.             cart.UpdateQuantity(e.RowIndex, q)
  13.         Catch ex As FormatException
  14.             e.Cancel = True
  15.         End Try
  16.         GridView1.EditIndex = -1
  17.         GridView1.DataBind()
  18.     End Sub
  19.  
[SHOPPINGCART.ASPX.VB]
Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System.Collections.Generic
  3.  
  4. Public Class ShoppingCart
  5.     Private _cart As List(Of CartItem)
  6.  
  7.     Public Sub New()
  8.         _cart = New List(Of CartItem)()
  9.     End Sub
  10.  
  11.     Public Function GetItems() As List(Of CartItem)
  12.         Return _cart
  13.     End Function
  14.  
  15.     Public Sub AddItem(ByVal id As String, ByVal name As String, ByVal price As Decimal)
  16.         Dim itemFound As Boolean = False
  17.         For Each item As CartItem In _cart
  18.             If item.ID = id Then
  19.                 item.Quantity += 1
  20.                 itemFound = True
  21.             End If
  22.         Next
  23.         If Not itemFound Then
  24.             Dim item As CartItem
  25.             item = New CartItem(id, name, price, 1)
  26.             _cart.Add(item)
  27.         End If
  28.     End Sub
  29.  
  30.     Public Sub UpdateQuantity(ByVal Index As Integer, ByVal quantity As Integer)
  31.         Dim item As CartItem
  32.         item = _cart(Index)
  33.         item.Quantity = quantity
  34.     End Sub
  35.  
  36.     Public Sub DeleteItem(ByVal index As Integer)
  37.         _cart.RemoveAt(index)
  38.     End Sub
  39.  
  40.     Public ReadOnly Property Count() As Integer
  41.         Get
  42.             Return _cart.Count
  43.         End Get
  44.     End Property
  45.  
  46. End Class
  47.  
Oct 28 '13 #1

✓ answered by Frinavale

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.

Expand|Select|Wrap|Line Numbers
  1. Dim t As TextBox = DirectCast( DirectCast( sender,GridViewRow).FindControl("TextBox1"), TextBox)

Expand|Select|Wrap|Line Numbers
  1. Dim t As TextBox = CType( CType( sender,GridViewRow).FindControl("TextBox1"), TextBox)

-Frinny

Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Consider using the FindControl method to retrieve your TextBox from the GridViewRow.

For example:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. TextBox  t = (TextBox)((GridViewRow) sender).FindControl("TextBox1"));
-Frinny
Oct 28 '13 #2

tsubasa
P: 64
I will give this a try. Thanks for the technical advise.

-Tsu
Oct 28 '13 #3

tsubasa
P: 64
Hello Frinny,

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

Frinavale
Expert Mod 5K+
P: 9,731
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.

Expand|Select|Wrap|Line Numbers
  1. Dim t As TextBox = DirectCast( DirectCast( sender,GridViewRow).FindControl("TextBox1"), TextBox)

Expand|Select|Wrap|Line Numbers
  1. Dim t As TextBox = CType( CType( sender,GridViewRow).FindControl("TextBox1"), TextBox)

-Frinny
Oct 29 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.