NorthArrow
asked on
Error: Index was outside the bounds of the array
Hi,
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.IndexOutOfRangeExce ption: 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.as px.vb Line: 41
Stack Trace:
[IndexOutOfRangeException: Index was outside the bounds of the array.]
System.Data.SqlClient.SqlD ataReader. ReadColumn (Int32 i, Boolean setTimeout) +777942
System.Data.SqlClient.SqlD ataReader. GetValueIn ternal(Int 32 i) +20
System.Data.SqlClient.SqlD ataReader. GetValue(I nt32 i) +82
System.Data.SqlClient.SqlD ataReader. get_Item(I nt32 i) +10
request.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\username\My Documents\Visual Studio 2008\WebSites\Prototype_I\ request.as px.vb:41
System.Web.UI.Control.OnLo ad(EventAr gs e) +99
System.Web.UI.Control.Load Recursive( ) +50
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +627
Here is my vb code:
Dim dbreader As SqlDataReader = dbcmd.ExecuteReader
If dbreader.HasRows Then
dbreader.Read()
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>).
Thanks!
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.IndexOutOfRangeExce
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\
Stack Trace:
[IndexOutOfRangeException:
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
request.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\username\My Documents\Visual Studio 2008\WebSites\Prototype_I\
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
Here is my vb code:
Dim dbreader As SqlDataReader = dbcmd.ExecuteReader
If dbreader.HasRows Then
dbreader.Read()
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>).
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Like this?
lbl_ManagerName.Text = dbreader.Item(15) & " " + dbreader.Item(17)
lbl_ManagerName.Text = dbreader.Item(15) & " " + dbreader.Item(17)
Yep!
ASKER
Thanks!
ASKER