Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2008, 04:03 PM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default System.IndexOutOfRangeException

I'm having a problem. I'm getting this error and I don't know how to fix it. It keeps throwing the error where the red is. Can someone help me.

System.IndexOutOfRangeException: Index was outside the bounds of the array.


Private Sub BindState2(ByVal ddl As DropDownList)
        connFTS = New SqlConnection(Application("connFTS"))
        Dim cmdSelect As SqlCommand = New SqlCommand("Select vchCompany from tblFWPurchaseCompany Inner join tblFWBusiness on tblFWPurchaseCompany.vchFWRecid = tblFWBusiness.vchFWRecid where vchcompany <> ''", connFTS)
        Dim dtrState As SqlDataReader

        Try
            connFTS.Open()
            dtrState = cmdSelect.ExecuteReader

            While dtrState.Read
                Dim newListItem As New ListItem
                newListItem.Value = dtrState.GetString(0)
                newListItem.Text = dtrState.GetString(2)
            ddl.Items.Add(newListItem)
            End While

            dtrState.Close()
            connFTS.Close()

        Catch er As Exception
            LogError(er.ToString, "BindState2")
            Exit Try
        Finally
            connFTS.Close()
        End Try
    End Sub

 
Old February 20th, 2008, 04:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This error: System.IndexOutOfRangeException: Index was outside the bounds of the array

means just what it says: Your index was outside the bounds of the array. I suggest changing newListItem.Text = dtrState.GetString(2) to newListItem.Text = dtrState.GetString(1). (As a side note: I prefer referring to a DataReader's elements by column names as opposed to numerical index's as this can cause you problems if, in the future, you add more columns to your query.)

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 20th, 2008, 04:11 PM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did have one right there at first but that didn't work. How would you use the columns instead of indexes. could you give me an example?

 
Old February 20th, 2008, 04:18 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Something like this: Convert.ToString(dtrState("<columnName">))

Reference Material:
http://www.google.com/search?source=...t+-+datareader

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 20th, 2008, 04:29 PM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IndexOutOfRangeException Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 13 May 31st, 2007 06:15 PM
Insert System date and System Time -Form _TextBox cnkumar74 VB How-To 14 February 14th, 2007 10:52 AM
System.IndexOutOfRangeException: - Please Help!!! olud ASP.NET 1.0 and 1.1 Basics 1 January 30th, 2007 02:56 PM
IndexOutOfRangeException - Chp 13, Page 446 aleahy BOOK: Beginning ASP.NET 1.0 1 November 5th, 2003 08:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.