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

Error: Index was outside the bounds of the array


I'm getting the following error when I try to populate a label control in vb.net.  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>).

  • 3
  • 2
1 Solution
Daniel WilsonCommented:
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)
NorthArrowAuthor Commented:
Thanks Daniel, That worked!  I need to put a space between the first and last name.  How is that coded?
NorthArrowAuthor Commented:
Like this?

lbl_ManagerName.Text = dbreader.Item(15) & " " + dbreader.Item(17)
Daniel WilsonCommented:
NorthArrowAuthor Commented:

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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