ispcorp
asked on
Argument 'Index' is not a valid value
Anybody know why i'm getting this error...
"Argument 'Index' is not a valid value"
There is a string value that its passing to it, I check that...
Private Function getSessionProduct(ByVal paramSessionCol As Collection, ByVal paramProductCol As Collection) As Collection
Dim colSessionProduct As Collection
Dim intCtr As Int32
Dim itSize As Int32 = paramSessionCol.Count
Dim objSessionElement As element
Dim objElement As element
For intCtr = 1 To itSize
colSessionProduct = paramSessionCol.Item(intCt r)
objSessionElement = colSessionProduct.Item(1)
objElement = paramProductCol.Item(objSe ssionEleme nt.Product Code) <----THIS IS WHERE THE ERROR IS HAPPENING
If Not objElement Is Nothing Then
getSessionProduct = colSessionProduct
Exit For
End If
Next
getSessionProduct = Nothing
End Function
Private Sub
"Argument 'Index' is not a valid value"
There is a string value that its passing to it, I check that...
Private Function getSessionProduct(ByVal paramSessionCol As Collection, ByVal paramProductCol As Collection) As Collection
Dim colSessionProduct As Collection
Dim intCtr As Int32
Dim itSize As Int32 = paramSessionCol.Count
Dim objSessionElement As element
Dim objElement As element
For intCtr = 1 To itSize
colSessionProduct = paramSessionCol.Item(intCt
objSessionElement = colSessionProduct.Item(1)
objElement = paramProductCol.Item(objSe
If Not objElement Is Nothing Then
getSessionProduct = colSessionProduct
Exit For
End If
Next
getSessionProduct = Nothing
End Function
Private Sub
objSessionElement.ProductC ode returns an integer?
Change your For loop like following:
For intCtr = 0 To itSize - 1
-tushar
For intCtr = 0 To itSize - 1
-tushar
ASKER
objSessionElemeent.Product Code returns a string
Tusharashah...
An index of a collection can never have an index of 0, it blows up if I pass intCtr=0 into it.
Tusharashah...
An index of a collection can never have an index of 0, it blows up if I pass intCtr=0 into it.
hmmm...could you please post the code for the Item() method. This might provide more clues as to what is genrating this error. My first gut reaction is that it's expecting a type integer but it's being a fred a type of string.
paramProductCol.Item(objSe ssionEleme nt.Product Code)
paramProductCol.Item(objSe
use Items, that the default Property Name
=======
objElement = paramProductCol.Items(objS essionElem ent.Produc tCode),
you could also do this:
objElement = paramProductCol(objSession Element.Pr oductCode)
=======
objElement = paramProductCol.Items(objS
you could also do this:
objElement = paramProductCol(objSession
ASKER
Item is a function within the Collection object. There is not method for it.
b1xml2...I don't see the difference, but I'll try it.
b1xml2...I don't see the difference, but I'll try it.
>>objElement = paramProductCol.Item(objSe ssionEleme nt.Product Code)
u will get this error when objSessionElement.ProductC ode returns an invalid key i.e. no item found in the collection for the key
u will get this error when objSessionElement.ProductC
ok .. i tried this out to validate what i had mentioned above
in page load
Dim x As New Collection
x.Add("s", "1")
x.Add("s1", "2")
Response.Write(x.Item("2") )
Prints "s1"
Now changed the code to search for a collection item with an invalid key
Dim x As New Collection
x.Add("s", "1")
x.Add("s1", "2")
Response.Write(x.Item("3") )
got the error
Argument 'Index' is not a valid value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Argument 'Index' is not a valid value.
in page load
Dim x As New Collection
x.Add("s", "1")
x.Add("s1", "2")
Response.Write(x.Item("2")
Prints "s1"
Now changed the code to search for a collection item with an invalid key
Dim x As New Collection
x.Add("s", "1")
x.Add("s1", "2")
Response.Write(x.Item("3")
got the error
Argument 'Index' is not a valid value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Argument 'Index' is not a valid value.
ASKER
I know, isn't there anything that would cause this error not to happen if an item is not found. I know in a HashMap you can check to see if it exists. I finally got a work around where I do a Try Catch, in the case where it can't find it, it just continues with the variable being NOTHING. There has got to be a better way, though...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, Rejojohny....Your right...I thought there might be another way, with out kicking off an error...Thanks