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>