Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2004, 06:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default Index out of range form question

Can somebody please explain to me why this is happening. I have a form like a shopping cart with the product name, quantity text field. The deli has 3 quantity fields while the bakery products only have one. This will update in a database once I figure out my problem.

This is whats happening, if I have 3 deli products everything writes out fine. Next I add a bakery product and then another deli product and I receive the following error.

Request object error 'ASP 0105 : 80004005'
Index out of range
/update_qty.asp, line 165
An array index is out of range.


For iProductCount=1 to Request.Form("fBasket").Count
If Request.Form("dept")(iProductCount) = "deli" Then
Response.Write Request.Form("dept")(iProductCount) & "Dept<BR>"
Response.Write Request.Form("fQuantity_01")(iProductCount) & "QTY-1<BR>"
Response.Write Request.Form("fQuantity_02")(iProductCount) & "QTY-2<BR>"
Response.Write Request.Form("fQuantity_03")(iProductCount) & "QTY-3<BR>"
Response.Write Request.Form("fBasket")(iProductCount) & "--ID<BR><BR>"
Else
Response.Write Request.Form("dept")(iProductCount) & "Dept<BR>"
Response.Write Request.Form("fQuantity_01")(iProductCount) & "QTY-1<BR>"
Response.Write Request.Form("fBasket")(iProductCount) & "--ID<BR><BR>"
End If
Next

Thanks In Advance
Mike Moore
__________________
Peace
Mike
http://www.eclecticpixel.com
 
Old February 24th, 2004, 12:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I suspect the problem you are having has to do with the conditional creation of form elements. For the first 3 deli products you create the 5 elements (dept, qty1, qty2, qty3, basket). The bakery product only creates 3 of those (all but qty2 and qty3). So in the end the array length for qty1 is different from qty2 and qty3.

Consider just the quantity fields. Here's a table of the array indexes for each of the resulting values of those fields for each product:
Code:
        |- Indexes --|
Item    qty1 qty2 qty3
Deli1   0    0    0
Deli2   1    1    1
Deli3   2    2    2
Bakery1 3    -    -
Deli4   4    3    3
So when you loop thru the items and you get to deli item 4, you try to get qty2 field index 4 which doesn't exist, thus the out of range error.

The solution to this problem is to always draw out all possible fields for a product regardless if you actually need to use them. In the case of a bakery product you don't need to use the qty2 and qty3 fields, but it does no harm to have them there because you can just ignore them. But more importantly, they are required to keep your arrays in sync.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative raghavendra_das ASP.NET 2.0 Professional 0 July 24th, 2008 08:16 AM
Crystal reports error : Index was out of range. vhrao Crystal Reports 0 October 13th, 2006 12:00 PM
index out of range datagrid muchengeti ASP.NET 2.0 Professional 0 April 21st, 2006 12:43 PM
Index Out of range exception PbsiGuru General .NET 3 March 22nd, 2004 11:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.