473,856 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IndexOutOfBound s on databinding

I'm writing what should be a very simple app against an Oracle database. The
app has a number of user controls, any one of which is loaded into a main
display page using the loadControl method, depending on which menu item a
user selects. Each of these controls follows the same basic pattern: Get a
dataset from the database and then display the results using basic
databinding.

Everything works fine except that I'll occaisionally get an IndexOutOfBound s
exception on an actual databinding command in the .ascx page. Here's an
example of the exception message:

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

Source Error:
Line 21: </td>
Line 22: <td class="value">
Line 23: <%# CurrentRow["APPRV_COLLAT_V IN"] %>
Line 24: </td>
Line 25: <td class="label">
Source File: C:\inetpub\wwwr oot\cart\Contro ls\CollateralAn dFinanceData.as cx
Line: 23

Stack Trace:
[IndexOutOfRange Exception: Index was outside the bounds of the array.]
System.Web.UI.D ataBoundLiteral Control.SetData BoundString(Int 32 index,
String s) +14
ASP.CollateralA ndFinanceData_a scx.__DataBind_ _control2(Objec t sender,
EventArgs e) in
C:\inetpub\wwwr oot\cart\Contro ls\CollateralAn dFinanceData.as cx:23
System.Web.UI.C ontrol.OnDataBi nding(EventArgs e) +66
System.Web.UI.C ontrol.DataBind () +26
System.Web.UI.C ontrol.DataBind () +86
CART.Controls.C ollateralAndFin anceData.GetDat a() +130
CART.Controls.C ollateralAndFin anceData.Page_L oad(Object sender, EventArgs
e) +5
System.Web.UI.C ontrol.OnLoad(E ventArgs e) +67
System.Web.UI.C ontrol.LoadRecu rsive() +35
System.Web.UI.C ontrol.AddedCon trol(Control control, Int32 index) +307
System.Web.UI.C ontrolCollectio n.Add(Control child) +153
CART.ViewApplic ation.set_Curre ntControlName(S tring value) +112
CART.Navigation Link.linkButton _Click(Object sender, EventArgs e) +34
System.Web.UI.W ebControls.Link Button.OnClick( EventArgs e) +108

System.Web.UI.W ebControls.Link Button.System.W eb.UI.IPostBack EventHandler.Ra isePostBackEven t(String eventArgument) +57
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +138
System.Web.UI.P age.ProcessRequ estMain() +1292
Notice that this is NOT the exception that's thrown if the field doesn't
exist in the row, nor the exception that would be raised if I was attempting
to access a row that doesn't exist. I've actually tried various ways of
getting to the field in the .cs code and writing the value to the trace. The
field is DEFINITELY there. The exception is actually on the
DataBoundLitera lControl.SetDat aBoundString method.

What testing I've done seems to indicate that there's some sort of upper
limit to a page's (or other objects???) number of databound elements. I can
wrap various sections within panels, and this has often solved the problem.
That reinforces my theory that I'm running into a control's limitations --
each panel now has a fewer number of databound controls and the page now has
none itself.

Today I ran into the problem on yet another page, so I broke the page into
panels. What's interesting is that NOW I'm suddenly getting the error on yet
ANOTHER page (one that worked fine before), and it's on the 3rd databound
field. Now I'm thinking I'm running into some sort of limit on the total
number of databound fields available for the session, or some other sort of
limitation weirdness.

On another note (and possibly should be another thread) -- when I do break
one of these .ascx controls into panels, I've found the <asp:Panel ... tag
can't be the first thing on the control, or I get a viewstate error.

Just as a sideline, I'm no newbie. I'm a senior developer and write this
kind of code day in and day out. This is the first app on which I've
encountered this kind of bizarre behaviour.

Some snippets from a standard control in my site:

From the .cs code behind:
...
public class ApplicantData : System.Web.UI.U serControl
{

private DataSet _data;

private void Page_Load(objec t sender, System.EventArg s e)
{
GetData();
}

private void GetData()
{
DataBase db = new DataBase();
_data = db.GetData("Get ApplicantData") ;

this.DataBind() ;
}

public DataRow CurrentRow
{
get
{
DataRow returnRow = null;
if (_data == null)
{
GetData();
}

if (_data.Tables.C ount > 0 && _data.Tables[0].Rows.Count > 0)
{
returnRow = _data.Tables[0].Rows[0];
}

return returnRow;
}
}
...

From the .ascx page:

<%@ Control Language="c#" AutoEventWireup ="false"
Codebehind="App licantData.ascx .cs" Inherits="CART. Controls.Applic antData"
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5"%>
<h2>Personal</h2>
<table class="dataTabl e" cellspacing="0" >
<tr>
<td class="label">
First Name
</td>
<td class="value">
<%# CurrentRow["APLNT_FIRST_NM "] %>
</td>
<td class="label">
SSN
</td>
<td class="value">
<%# CurrentRow["APLNT_SSN"] %>
</td>
</tr>
<td class="label">
Middle Initial
</td>
<td class="value">
<%# CurrentRow["APLNT_MIDL _NM"] %>
</td>
<td class="label">
DOB
</td>
<td class="value">
<%# String.Format(" {0:d}", CurrentRow["APLNT_BIRTH_DT "]) %>
</td>
<tr>
<td class="label">
Last Name
</td>
<td class="value">
<%# CurrentRow["APLNT_LAST _NM"] %>
</td>
<td class="label">
Age
</td>
<td class="value">
<%# CurrentRow["APLNT_AGE"] %>
</td>
</tr>
<tr>
<td class="label">
Suffix
</td>
<td class="value">
<%# CurrentRow["APLNT_SUFFX_NM "] %>
</td>
<td class="label">
Marital Status
</td>
<td class="value">
<%# CurrentRow["APLNT_MARITAL_ STATUS_CD"] %>
</td>
</tr>
</table>
Nov 19 '05 #1
3 3168
Hi kevin,

Welcome to ASP.NET newsgroup.
From your description and the code snippet you provided, you've expose an
DataRow property on a ascx UserControl which is used to perform databinding
to some inline <%# .. %> blocks and you perform the databinding in the
Usercontrol's Page_Load event. However, you're encountering occasional
"IndexOutOfRang eException" when you dynamically add this Usercontrol on
asp.net page,yes?

From my opinion , the problems is still likely something incorrect with the
DataSource (you expose through the CurrentRow). At least there won't have
limit on the total number of databound fields available per page or
Session. How many UserControl or DtaBinding fields are there on your
UserControl?

Since we can't get any further info through the exception and callstack, I
suggest you turn on your web application's Trace (in the <trace> element
in web.config).

And put Trace.Write statement during your UserControl's databinding code,
such as GetData()
CurrentRow's Get method .... You can check the _data or the DataRow or
event all those fields in the row to see what happend actually.

BTW, from the code you provided , your data access logic seems like:

=============== ====
private void Page_Load(objec t sender, System.EventArg s e)
{
GetData();
}

private void GetData()
{
DataBase db = new DataBase();
_data = db.GetData("Get ApplicantData") ;

this.DataBind() ;
}

public DataRow CurrentRow
{
get
{
DataRow returnRow = null;
if (_data == null)
{
GetData();
}
............... ..

}
=============== ==

I don't think we should put "this.DataBind( );" in "GetData" function since
it will make all the databinding statement be fired again. Maybe we can
change it to:

=============== ==
private void Page_Load(objec t sender, System.EventArg s e)
{
this.DataBind() ;
}

private void GetData()
{
DataBase db = new DataBase();
_data = db.GetData("Get ApplicantData") ;

}

public DataRow CurrentRow
{
get
{
DataRow returnRow = null;
if (_data == null)
{
GetData();
}
............... ..

}

=============== ==

HTH. Thanks,
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #2
Steven,

Thanks for your response!
Since we can't get any further info through the exception and callstack, I
suggest you turn on your web application's Trace (in the <trace> element
in web.config).

And put Trace.Write statement during your UserControl's databinding code,
such as GetData()
CurrentRow's Get method .... You can check the _data or the DataRow or
event all those fields in the row to see what happend actually.
I did something somewhat similar to this. In my trace, I actually looped
through the dataset independently of the databinding and writing out the
values of the fields. All the fields traced out correctly and there were no
errors, but the same error occurred in the inline code. As an act of
desperation, I then went through all of the databinding code and replaced the
simple <%# %> blocks with literal controls:

old:
<td>
<%# CurrentRow["APLNT_LAST _NM"] %>
</td>

new:

<td>
<asp:Literal runat="server" id="lastNameLit eral" text='<%#
CurrentRow["APLNT_LAST _NM"] %>'></asp:Literal>
</td>

That completely solved my problem. On the one hand, that means I'm cool now
and no longer in need of assistance. On the other hand, I think that
reinforces the theory that there is something amiss behind the scenes when
binding directly to a user control or aspx page. I only insist in this
because it might be helpful for others who experience the same issue, even if
it's released as some sort of best practice or guideline.

For example, when I first encountered the issue, I solved the issue on one
page SIMPLY by wrapping two tables like this one:

<table>
<tr>
<td>Last Name:</td>
<td>
<%# CurrentRow["APLNT_LAST _NM"] %>
</td>
</tr>
<!-- several more rows with other fields -->
</table>

In their own panel controls, like this:

<asp:Panel runat="server" id="tableOnePan el">
<table>
<tr>
<td>Last Name:</td>
<td>
<%# CurrentRow["APLNT_LAST _NM"] %>
</td>
</tr>
<!-- several more rows with other fields -->
</table>
</asp:Panel>

This solved the problem on that page immediately, but then I would run into
the same issue on another page with another field. As in the process that
finally resolved my issue completely (using literal controls instead of
binding directly to the page), the name of the fields never changed. In one
case I cut-and-pasted, and in the other case I never even touched the field
names -- I simply added the panel tags before and after the existing table.

One thing to keep in mind is the somewhat non-traditional architecture of
this specific application. It's writen almost like a Java-bean app, with one
main .aspx page that contains a single place holder control. The various
"pages" are all .asCx user controls just like the one we've been describing.
A navigation control contains link buttons. The onClick event of each link
button (on the server side) creates the specific ascx control via
LoadControl, and then adds the control to the place holder. So in the
original load of the page, the last control is added behind the scenes so
that the engine can handle events. Then on the post-back, the new control is
added to the place holder. So I guess in actuallity, the page now contains
direct binding to all the fields on both pages. My only conclusion is that
at some point I'm simply exceeding some kind of limit to the number of
databound elements the page can handle as direct child controls.

Any other thoughts are more than welcome! Again, I have definitely solved my
issue, and dont' require assistance, but I'd be more than happy to contine
disussing the underlying issue.

Thanks again,

Kevin

"Steven Cheng[MSFT]" wrote:
Hi kevin,

Welcome to ASP.NET newsgroup.
From your description and the code snippet you provided, you've expose an
DataRow property on a ascx UserControl which is used to perform databinding
to some inline <%# .. %> blocks and you perform the databinding in the
Usercontrol's Page_Load event. However, you're encountering occasional
"IndexOutOfRang eException" when you dynamically add this Usercontrol on
asp.net page,yes?

From my opinion , the problems is still likely something incorrect with the
DataSource (you expose through the CurrentRow). At least there won't have
limit on the total number of databound fields available per page or
Session. How many UserControl or DtaBinding fields are there on your
UserControl?

Since we can't get any further info through the exception and callstack, I
suggest you turn on your web application's Trace (in the <trace> element
in web.config).

And put Trace.Write statement during your UserControl's databinding code,
such as GetData()
CurrentRow's Get method .... You can check the _data or the DataRow or
event all those fields in the row to see what happend actually.

BTW, from the code you provided , your data access logic seems like:

=============== ====
private void Page_Load(objec t sender, System.EventArg s e)
{
GetData();
}

private void GetData()
{
DataBase db = new DataBase();
_data = db.GetData("Get ApplicantData") ;

this.DataBind() ;
}

public DataRow CurrentRow
{
get
{
DataRow returnRow = null;
if (_data == null)
{
GetData();
}
............... ..

}
=============== ==

I don't think we should put "this.DataBind( );" in "GetData" function since
it will make all the databinding statement be fired again. Maybe we can
change it to:

=============== ==
private void Page_Load(objec t sender, System.EventArg s e)
{
this.DataBind() ;
}

private void GetData()
{
DataBase db = new DataBase();
_data = db.GetData("Get ApplicantData") ;

}

public DataRow CurrentRow
{
get
{
DataRow returnRow = null;
if (_data == null)
{
GetData();
}
............... ..

}

=============== ==

HTH. Thanks,
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #3
Thanks for your detailed response and description Kevin,

AS you said that the problem went away when you use Literal Control instead
of directly inject databinding expression. I'm also feeling very strange.
From my research, the ASP.NET will actually use DataBoundLitera lControl to
display datas we bind directly with <%# %> expression. Those dynamic
generated controls are in the dynamic compiled page class (in the tempoaray
asp.net folder) so we have no idea of them generally. The code will be
something like:

private Control __BuildControl_ _control2()
{
DataBoundLitera lControl control1 = new DataBoundLitera lControl(8, 7);
this.__control2 = control1;
control1.SetSta ticString(0, "\r\n<h2>Person al Info</h2>\r\n<table
class=\"dataTab le\" cellspacing=\"0 \">\r\n\t<tr>\r \n\t\t<td
class=\"label\" >\r\n\t\t\tFirs t Name\r\n\t\t</td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(1,
"\r\n\t\t</td>\r\n\t</tr>\r\n\t<TR>\r \n\t\t<td
class=\"label\" >\r\n\t\t\tMidd le Initial\r\n\t\t </td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(2,
"\r\n\t\t</td>\r\n\t</TR>\r\n\t<tr>\r \n\t\t<td
class=\"label\" >\r\n\t\t\tLa st Name\r\n\t\t</td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(3, "\r\n\t\t</td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(4, "\r\n\t\t</td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(5,
"\r\n\t\t</td>\r\n\t</tr>\r\n\t\r\n\t <TR>\r\n\t\t< td
class=\"label\" >\r\n\t\t\tMidd le Initial\r\n\t\t </td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(6,
"\r\n\t\t</td>\r\n\t</TR>\r\n\t<TR>\r \n\t\t<td
class=\"label\" >\r\n\t\t\tMidd le Initial\r\n\t\t </td>\r\n\t\t<td
class=\"value\" >\r\n\t\t\t") ;
control1.SetSta ticString(7,
"\r\n\t\t</td>\r\n\t</TR>\r\n</table>\r\n");
control1.DataBi nding += new EventHandler(th is.__DataBind__ control2);
return control1;
}

public void __DataBind__con trol2(object sender, EventArgs e)
{
DataBoundLitera lControl control2 = (DataBoundLiter alControl) sender;
Control control1 = control2.Bindin gContainer;
control2.SetDat aBoundString(0,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_FIRST_NM "]));
control2.SetDat aBoundString(1,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_MIDL _NM"]));
control2.SetDat aBoundString(2,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_LAST _NM"]));
control2.SetDat aBoundString(3,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_LAST _NM"]));
control2.SetDat aBoundString(4,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_LAST _NM"]));
control2.SetDat aBoundString(5,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_MIDL _NM"]));
control2.SetDat aBoundString(6,
Convert.ToStrin g(base.get_Curr entRow()["APLNT_MIDL _NM"]));
}

We can use the reflector tool to lookup the dynamic generated page in the
assemblies
(in %SYSTEM%\Micros oft.NET\Framewo rk\v1.1.4322\Te mporary ASP.NET Files\
....)

So I think the indexOutOfRange exception just occur at the above step. One
thing we can do curently is try building a simple test page to repro the
problem. Would you try creating a simple asp.net page or ascx control which
just add some <%# %> blocks and use some test datas( generate on the fly
rather than from database) bind with those expression. If the error occurs,
you can have a look at the dyanmic page (or UserControl) class's code to
see whether it is incorrectly generated. If there does exists some
problems with the dynamic generated file, you can send me the repro page so
that I can send it to our dev guys for some further research.

Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1871
by: John Bailey | last post by:
When I first built a few web pages in ASP .Net 2.0, I thought it was great. The formview and detailview contorls would automatically layout the controls for you, the update methods were automatically generated, and the objectdatasource let you design against a business object through the gui. Now I am working on my first real web application on 2.0, and I find this automatic functionality doesn't do much beyond allowing me to generate...
4
1662
by: Nathan Sokalski | last post by:
I have two databinding expressions (the first & last names from a DB) that I want to assign to the text property of a Label so that the result is LASTNAME,FIRSTNAME. At the moment, I have the following which I know works when I use it by itself: text='<%# DataBinder.Eval(Container,"DataItem.membernames.lname") %>' What I need to do is somehow concatenate this databinding expression, a comma, and another databinding expression for the...
9
2119
by: Dennis | last post by:
I have tried using Databinding for my application but always seem to find it very restrictive (maybe I don't completely understand it enough). I always seem to find it much easier to display a form, have the user fill it out then put the data into a class representing a data row and then use the OLEadaptor or OLECommands to update the database. My question is has anyone really used databinding in anything but the simpliest application...
8
2193
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got answered... In 1.1 we always did our own myDataAdapter.fills and we liked that control for lots of good reasons. Now the new DataSource (or is it a TableAdapter:Dataset) automatically fills the Gridview. How can we control that fill? In a...
0
1333
by: Wayne Sepega | last post by:
I have the following Object DataSource <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCustomer" TypeName="Customers" DataObjectTypeName="Customer" InsertMethod="InsertCustomer" OnUpdated="ObjectDataSource1_Updated" UpdateMethod="UpdateCustomer" OnSelected="ObjectDataSource1_Selected">
9
24834
by: J055 | last post by:
Hi I have a very simple configuration of the GridView with paging and sorting. When I do a postback the DataBinding event fires twice - in both the ProcessPostData and PreRender stages of the page life cycle. In this example the event fires twice when a) GridView EnableViewState=False and any image type control in the <columns/> element. When either EnableViewState is set to true or the image button is removed, the event fires once....
1
14319
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. Both the grid and the dropdown box are retrieving and displaying data fine, I just cant bind the two together. I followed the instructions in the help document called 'Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web...
8
2097
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the generic type Field<T> (see sample code). public class Employee { public Field<stringForename
1
1636
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello to all, I want to know if DataBinding in asp.net 2,0 is better than to fill up the values of the controls of the following form: this.miControlTextBox.Text = valorParaControlTextbox; Performance with databinding is good in asp.net 2.0 ? Would you prefer: databinding or set value to Text property of control ?
0
9904
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11051
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10693
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10378
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9527
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5756
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.