[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4687
  • Last Modified:

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(intCtr)
            objSessionElement = colSessionProduct.Item(1)
            objElement = paramProductCol.Item(objSessionElement.ProductCode) <----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
0
ispcorp
Asked:
ispcorp
  • 4
  • 3
  • 2
  • +2
1 Solution
 
brdrokCommented:
objSessionElement.ProductCode  returns an integer?
0
 
tusharashahCommented:
Change your For loop like following:

For intCtr = 0 To itSize - 1

-tushar
0
 
ispcorpAuthor Commented:
objSessionElemeent.ProductCode 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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
brdrokCommented:
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(objSessionElement.ProductCode)
0
 
b1xml2Commented:
use Items, that the default Property Name
=======
objElement = paramProductCol.Items(objSessionElement.ProductCode),
you could also do this:

objElement = paramProductCol(objSessionElement.ProductCode)
0
 
ispcorpAuthor Commented:
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.
0
 
RejojohnyCommented:
>>objElement = paramProductCol.Item(objSessionElement.ProductCode)

u will get this error when objSessionElement.ProductCode returns an invalid key i.e. no item found in the collection for the key
0
 
RejojohnyCommented:
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.
0
 
ispcorpAuthor Commented:
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...
0
 
RejojohnyCommented:
there is no collection.contains  or any other method by which we can check if a particular item is exiting ..
>>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.

u could use try catch block as given below .. and trap the exact exception which is of the type "ArgumentException" ..
        Dim x As New Collection
        x.Add("s", "1")
        x.Add("s1", "2")
        Try
            Dim MyValue As String = x.Item("3")
        Catch ex As ArgumentException
            Response.Write("Key not found")
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

        response.write(MyValue)
0
 
ispcorpAuthor Commented:
Ok, Rejojohny....Your right...I thought there might be another way, with out kicking off an error...Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now