Error: Index was outside the bounds of the array

Posted on 2009-04-20
Last Modified: 2012-05-06

I'm getting the following error when I try to populate a label control in  Here is the error:

Server Error in '/Prototype_I' Application.

Index was outside the bounds of the array.
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.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

Line 39:             lbl_ZipCode.Text = dbreader.Item(6)
Line 40:             lbl_Building.Text = dbreader.Item(7)
Line 41:             lbl_ManagerName.Text = dbreader.Item(15 + 17)
Line 42:             'lbl_Personnel.Text = dbreader.Item(19)
Line 43:             'lbl_District.Text = dbreader.Item(20)

Source File: C:\Documents and Settings\username\My Documents\Visual Studio 2008\WebSites\Prototype_I\request.aspx.vb    Line: 41

Stack Trace:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout) +777942
   System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i) +20
   System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) +82
   System.Data.SqlClient.SqlDataReader.get_Item(Int32 i) +10
   request.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\username\My Documents\Visual Studio 2008\WebSites\Prototype_I\request.aspx.vb:41
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


Here is my vb code:

Dim dbreader As SqlDataReader = dbcmd.ExecuteReader
If dbreader.HasRows Then

lblUnit.Text = dbreader.Item(0)
lbl_BusinessName.Text = dbreader.Item(1)
lbl_MailCode.Text = dbreader.Item(2)


lbl_ManagerName.Text = dbreader.Item(15 + 17)
lbl_LevelAuth.Text = dbreader.Item(18)

What I am trying to do is input the first and last name of the Manager into one label "ManagerName".  Is there a way to concatenate the two columns to be displayed in one label using the datareader?

I have listed the datareader to populate the fields in an array based on the number of columns in the database table.  For instance, "Business Unit" is the first column in the database table, therefore, I am asking for the first value in the table to display in the first label (0) "Business Unit" and so on, until I get to column 15 and 17, which contain the First and Last name (respectively) of the manager.  I know I could use two labels and/or two <td>tags, but I've already coded the table in HTML and I would like to avoid having to add more cells to the table (each label is in one <td>).

Question by:NorthArrow
    LVL 32

    Accepted Solution

    lbl_ManagerName.Text = dbreader.Item(15 + 17) is the same as lbl_ManagerName.Text = dbreader.Item(32)

    What you want, I think is:
    lbl_ManagerName.Text = dbreader.Item(15) + dbreader.Item(17)

    Author Comment

    Thanks Daniel, That worked!  I need to put a space between the first and last name.  How is that coded?

    Author Comment

    Like this?

    lbl_ManagerName.Text = dbreader.Item(15) & " " + dbreader.Item(17)
    LVL 32

    Expert Comment

    by:Daniel Wilson

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now