Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index Outside the Bounds of the Array

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I'm trying to develop a control that will allow a page developer to use their own XML documents to populate a page's contents. As such, I'm trying to have the size and contents of an array populated dynamically, as opposed to hard-coded.

In the GetArrayContents method below, I'm trying to read the DataColumns from a DataTable and write a string value to be read by the ReadSlides method to populate a jagged array, but I keep getting a "Index Outside the Bounds of the Array" error. I thought this would be OK, but apparently not.

// set a jagged array of images & captions to use by reading data from an XML file
private string[][] ReadSlides(string filepath)
{
FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
DataSet ds = new DataSet();

ds.ReadXml(sr);
fs.Close();

DataTable table = ds.Tables[0];
DataRow[] rows = table.Select();

string[][] contents = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
// contents = new string[] { rows[&quot;image&quot;].ToString(),rows[&quot;alt&quot;].ToString(),rows[&quot;title&quot;].ToString(),rows[&quot;caption&quot;].ToString() };
contents = new string[] { GetArrayContents(table) };
}

return contents;
}

// helper method that automates the population of the jagged array
private string GetArrayContents(DataTable table)
{
string arrayContents = string.Empty;

foreach(DataColumn col in table.Columns)
{
arrayContents += &quot;rows[\&quot;&quot; + col.ColumnName + &quot;\&quot;].ToString(),&quot;;
}

return arrayContents;
}

Thanks!
 
HI John,

Good thought...but nada. Same thing.
 
Hi John,

FYI...I got it wotking...I had to return a string array in my private helper method:

////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
for(int i=0;i<rows.Length;i++)
{
contents = GetArrayContents(i,table,rows);
}

private string[] GetArrayContents(int rowNumber,DataTable table,DataRow[]
rows)
{
string[] arrayContents = new string[table.Columns.Count];
int incrementer = 0;

for(int i=0;i<table.Columns.Count;i++)
{
arrayContents.SetValue(rows[rowNumber].ToString(),i);
incrementer++;
}
return arrayContents;
}

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top