IndexOutOfRangeException With No Array

Posted on 2009-12-29
Last Modified: 2013-11-26
I am running into an exceptionally strange error.  It's an IndexOutOfRangeException, but the odd thing is that there isn't an array access statement anywhere NEAR where the error is occurring.  It's being thrown on a simple string assignment statement:

Private Function ReplaceProductTokens(ByVal template As String, ByVal productID As Integer, ByVal siteURL As String, ByVal optionText As String, ByRef oWeb As webClasses)
        Dim retValue As String = template   '''' ERROR IS THROWN ON THIS LINE ''''
        Dim sql As String = "select brand.*,product.*,departments.*,price.* from price inner join product on price.product_id=product.product_id inner join brand  on product.brand_id=brand.brand_id "
        sql &= "inner join departments on departments.dept_id=product.dept_id where product.product_id=" & productID.ToString()

        Dim lsResult As String = ""
        Dim oDT As Data.DataTable = oWeb.oFunctions.getRS(sql, lsResult)
        If lsResult = "" And oDT.Rows.Count > 0 Then
            Dim oRow As DataRow = oDT.Rows(0)
            Dim pageUrl As String = siteURL & oWeb.RemoveIllegalCharacters(oRow("dept_alt"))
            pageUrl &= "/" & oWeb.RemoveIllegalCharacters(oRow("brand")) & "-" & oWeb.RemoveIllegalCharacters(oRow("product_code"))
            retValue = retValue.Replace("[REQUEST_PRODINFO]", pageUrl & ".asp")
            retValue = retValue.Replace("[PRICE]", FormatCurrency(oRow("Sale"), 2))
            retValue = retValue.Replace("[MSRP]", FormatCurrency(oRow("Msrp"), 2))
            retValue = retValue.Replace("[PRODUCT_NAME]", oRow("product_name").ToString)
            retValue = retValue.Replace("[PRODUCT_CODE]", oRow("product_code").ToString)
            retValue = retValue.Replace("[PRODUCT_CODE_AND_BRAND]", oRow("brand").ToString & " " & oRow("product_code").ToString)
            retValue = retValue.Replace("[SUMMARY]", oRow("SpecsList").ToString.Trim)
            retValue = retValue.Replace("[CUSTOM_DESC]", oRow("CustomText").ToString.Trim)
            retValue = retValue.Replace("[EXTRA_DESC]", oRow("Caption").ToString.Trim)
            retValue = retValue.Replace("[IMAGE_BRAND]", siteURL & "brandimages/" & HttpUtility.UrlEncode(oRow("brand").ToString.Replace(" ", "") & ".jpg"))
            retValue = retValue.Replace("[STOCK_NUMBER]", oRow("part_no").ToString)
        End If

        Return retValue
End Function

Is there any reason why I might be getting that particular error in that place?
Question by:ShopVisibleRoger
    LVL 21

    Expert Comment

    it can be any of the lines...if yo're using vstudios, place a breakpoint on the firstline of this and debug...if not, comment out lines and see where the error occur...
    LVL 21

    Accepted Solution

    remember a string is just an array of chars, so retValue = retValue.Replace("[PRICE]", it could be  here...
    FormatCurrency(oRow("Sale"), 2))
     retValue = retValue.Replace("[MSRP]", FormatCurrency(oRow("Msrp"), 2))


     Dim oRow As DataRow = oDT.Rows(0)

    so i would debug with breakpoints  

    debug in VS by placing breakpoint with F9 key
    run with debug
    and F11 to see each line of code after the breakpoint has been hit...

    Author Closing Comment

    Yep, that was the problem.  The line number that the error message pointed to wasn't where the error was happening at all.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now