locked
System.IndexOutOfRangeException RRS feed

  • Question

  • User1753528066 posted

    I am working on modifying an existing application and have hit an error I don’t know how to fix.  I needed to add some fields to an existing sql db and modify some vb code and a stored procedure.

     <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

      When I research the definition of the error, the most common is “exception is thrown when an attempt is made to access an element of an array or collection with an index that is outside the bounds of the array or less than zero”.  The error is raised on the first item I added so it makes sense I failed to modify some addition code, but I am not sure where to look.

     <o:p></o:p>I can not find anywhere in the application where any array or collection even exists(other than the db).<o:p></o:p> <o:p></o:p>Below is some of the code.  If anyone could point me in the right direction, I would really appreciate it.<o:p></o:p> <o:p></o:p>

    Thanks

     

    The error:<o:p> </o:p>

    Exception Details: System.IndexOutOfRangeException: ItemNumber

    Source Error:

    Line 382:                _categoryId = CType(reader("categoryId"), Integer)
    Line 383:            End If
    Line 384:            If Not IsDBNull(reader("ItemNumber")) Then
    Line 385:                _ItemNumber = CType(reader("itemNumber"), String)
    Line 386:            End If

     

     The section of code from the error section:

    ''' <summary>

    ''' Initializes a product from a DataReader

    ''' </summary>

    Public Sub New(ByVal reader As SqlDataReader)

    _id = CType(reader("Id"), Integer)

    _name = CType(reader("Name"), String)

    _isFeatured = CType(reader("IsFeatured"), Boolean)

    _price = CType(reader("Price"), Decimal)

    _description = CType(reader("Description"), String)

    If Not IsDBNull(reader("FullDescription")) Then

    _fullDescription = CType(reader("fullDescription"), String)

    End If

    _hasImage = Not ISDBNull(reader("ImageSize"))

    If Not IsDBNull(reader("ImageAltText")) Then

    _imageAltText = CType(reader("ImageAltText"), String)

    End If

    If Not ISDBNull(reader("CategoryId")) Then

    _categoryId = CType(reader("categoryId"), Integer)

    End If

    If Not IsDBNull(reader("ItemNumber")) Then

    _ItemNumber = CType(reader("itemNumber"), String)

    End If

    If Not IsDBNull(reader("SalePrice")) Then

    _SalePrice = CType(reader("SalePrice"), Decimal)

    End If

    If Not IsDBNull(reader("MaterialCost")) Then

    _MaterialCost = CType(reader("MaterialCost"), Decimal)

    End If

    If Not IsDBNull(reader("DateAdded")) Then

    _DateAdded = CType(reader("DateAdded"), Date)

    End If

    If Not IsDBNull(reader("DateSold")) Then

    _DateSold = CType(reader("DateSold"), Date)

    End If

    If Not IsDBNull(reader("CategoryTitle")) Then

    _categoryTitle = CType(reader("CategoryTitle"), String)

    End If

    End Sub

     

    The stored procedure:

    ALTER PROCEDURE ProductInsert

    (

    @CategoryId
    INT,

    @Name NVarchar(200),

    @IsFeatured BIT,

    @Price Money,

    @Description NVarchar(max),

    @FullDescription NVarchar(max),

    @ImageAltText NVarchar(150),

    @ItemNumber Nchar(10),

    @SalePrice Money,

    @DateAdded datetime,

    @DateSold datetime,

    @MaterialCost Money

    )

    AS

    INSERT
    Products

    (

    CategoryId,

    Name,

    IsFeatured,

    Price,

    Description,

    FullDescription,

    ImageAltText,

    ItemNumber,

    SalePrice,

    DateAdded,

    DateSold,

    MaterialCost

    )

    VALUES

    (

    @CategoryId,

    @Name,

    @IsFeatured,

    @Price,

    @Description,

    @FullDescription,

    @ImageAltText,

    @ItemNumber,

    @SalePrice,

    @DateAdded,

    @DateSold,

    @MaterialCost

    )

     

    RETURN @@IDENTITY

     

    Sunday, December 30, 2007 1:54 PM

Answers

  • User-319574463 posted

     I suggest that you check if ItemNumber is contained in the DataReader and exactly how it is capitalised.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 2:12 PM
  • User-158764254 posted

    You did not post the sql select statement that you used to create the datareader.

    Did you include the column name "ItemNumber" as part of the select statement?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 3:10 PM
  • User1612051050 posted

    It looks like ItemNumber didn't get returned to the reader. Unrelated but if ItemNumber (nchar[10]) is merely a zero-length string rather than a null then IsDBNull won't catch it. Also remember that null != DBNull. For us C# developers we have to check for possible null SqlTypes against object DBNull.Value:

    // fetch a SqlDataReader instance "dr"
    if (null != dr) {
        while (dr.Read()) {
            int ID = (DBNull.Value != dr["ID"]) ? (int)dr["ID"] : 0;
        }
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 3:37 PM

All replies

  • User-319574463 posted

     I suggest that you check if ItemNumber is contained in the DataReader and exactly how it is capitalised.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 2:12 PM
  • User-158764254 posted

    You did not post the sql select statement that you used to create the datareader.

    Did you include the column name "ItemNumber" as part of the select statement?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 3:10 PM
  • User1612051050 posted

    It looks like ItemNumber didn't get returned to the reader. Unrelated but if ItemNumber (nchar[10]) is merely a zero-length string rather than a null then IsDBNull won't catch it. Also remember that null != DBNull. For us C# developers we have to check for possible null SqlTypes against object DBNull.Value:

    // fetch a SqlDataReader instance "dr"
    if (null != dr) {
        while (dr.Read()) {
            int ID = (DBNull.Value != dr["ID"]) ? (int)dr["ID"] : 0;
        }
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 30, 2007 3:37 PM