[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

IndexOutOfRangeException With No Array

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?
  • 2
1 Solution
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...
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...
ShopVisibleRogerAuthor Commented:
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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