More of "Index was outside the bounds of the array"

A

Antoine

Herfried and Cor:-

I used tracing and actually tracked down the code that was causing the
problem most likely. I wonder if you wanted to comment on it.

Also I wonder if there is a better way of testing if there is data than
testing the length of the xml string I used as stringreader to create the
dataset, but thats a side issue.

I think I tried isdbnull and is nothing and stuff like that and they cause
run time errors if there isnt any data, in other areas of code I have had
problems with using it so found this a reasonable way of ensuring the string
returned with data that must mean there was data - as I have a catch anyway
so if any errors occur, it will quit before it gets to these string length
testing parts of code.

Anyway back to the actual problem. I THINK its because the application
usually throws a comma in the field1, but I can reproduce the error if I
don't.
I THINK I either need to change my selection of fields (and get less
complicated and get data from seperate fields that might contain same data )
or test for an index of comma in the file, and do something different if
there is no comma.

If Len(xmlstr1) > 25 Then
For intCounter = 0 To dataset1.Tables("result").Rows.Count - 1
dt.Rows.Add(CreateRow("[" +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(1).subs
tring(1, 1).toupper +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(0).subs
tring(0, 1).toupper + "]:" +
dataset1.Tables("result").Rows(intCounter).Item("field1"),
dataset1.Tables("result").Rows(intCounter).Item("field2"), dt))
Next
End If


"An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the exception
can be identified using the exception stack trace below.

Stack Trace:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
System.Array.InternalGetValue(Int32 index1, Int32 index2, Int32 index3)
+0
System.Array.GetValue(Int32 index) +32
Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet(Object o,
Object[] args, String[] paramnames) +187
sharedcal.ShareCal.o() +655
sharedcal.ShareCal.Page_Load(Object o, EventArgs e) +799
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
 
K

Ken Tucker [MVP]

Hi,

Two ways it could fail with that error. 1 no comma in string split
will only create an item 0 in the array. Second the first letter in a
string is at position 0 so if the string isnt 2 characters long
substring(1,1) will fail. It is also better to use & to combine strings
than +.

Dim a() As String = "No commas here".Split(",".ToCharArray)

Debug.WriteLine(a(1)) ' Index outside of bounds error here



Ken
---------------------
Herfried and Cor:-

I used tracing and actually tracked down the code that was causing the
problem most likely. I wonder if you wanted to comment on it.

Also I wonder if there is a better way of testing if there is data than
testing the length of the xml string I used as stringreader to create the
dataset, but thats a side issue.

I think I tried isdbnull and is nothing and stuff like that and they cause
run time errors if there isnt any data, in other areas of code I have had
problems with using it so found this a reasonable way of ensuring the string
returned with data that must mean there was data - as I have a catch anyway
so if any errors occur, it will quit before it gets to these string length
testing parts of code.

Anyway back to the actual problem. I THINK its because the application
usually throws a comma in the field1, but I can reproduce the error if I
don't.
I THINK I either need to change my selection of fields (and get less
complicated and get data from seperate fields that might contain same data )
or test for an index of comma in the file, and do something different if
there is no comma.

If Len(xmlstr1) > 25 Then
For intCounter = 0 To dataset1.Tables("result").Rows.Count - 1
dt.Rows.Add(CreateRow("[" +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(1).subs
tring(1, 1).toupper +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(0).subs
tring(0, 1).toupper + "]:" +
dataset1.Tables("result").Rows(intCounter).Item("field1"),
dataset1.Tables("result").Rows(intCounter).Item("field2"), dt))
Next
End If


"An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the exception
can be identified using the exception stack trace below.

Stack Trace:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
System.Array.InternalGetValue(Int32 index1, Int32 index2, Int32 index3)
+0
System.Array.GetValue(Int32 index) +32
Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet(Object o,
Object[] args, String[] paramnames) +187
sharedcal.ShareCal.o() +655
sharedcal.ShareCal.Page_Load(Object o, EventArgs e) +799
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
 
C

Cor Ligthert

Antoine,

In advance I think that it is for yourself not good to address these
questions to me alone (I cannot speak for Herfried).

Some things can easily be answered by me, others questions by others,
therefore it is a community, so it is better not to address questions to
special people.

When I was you I would place this question again, without that Cor and
Herfried to give yourself a better change. And than show what is that
createrow, because I don't know that as a function to create a datarow.
(However maybe others do)

Just to help nothing offended or whatever,

Cor
 
A

Antoine

Cor Ligthert said:
Antoine,

In advance I think that it is for yourself not good to address these
questions to me alone (I cannot speak for Herfried).

Some things can easily be answered by me, others questions by others,
therefore it is a community, so it is better not to address questions to
special people.

When I was you I would place this question again, without that Cor and
Herfried to give yourself a better change. And than show what is that
createrow, because I don't know that as a function to create a datarow.
(However maybe others do)

Just to help nothing offended or whatever,

Cor

Absolutely none taken.

Thanks for you advice, I thik I understand now to redo the code anyway, and
access a completely differnent call that allows seperate fields. The one I
was doing only brought out a database "combined" field, which of course with
such combinations would just lead to complicated code.

Thanks!
 
A

Antoine

Thanks I will take to heart your info particulary about using &

I thik I understand now to redo the code anyway, and access a completely
differnent call that allows seperate fields. The one I was doing only
brought out a database "combined" field, which of course with such
combinations would just lead to complicated code because of combinations of
"," white space etc etc

Thanks!

Ken Tucker said:
Hi,

Two ways it could fail with that error. 1 no comma in string split
will only create an item 0 in the array. Second the first letter in a
string is at position 0 so if the string isnt 2 characters long
substring(1,1) will fail. It is also better to use & to combine strings
than +.

Dim a() As String = "No commas here".Split(",".ToCharArray)

Debug.WriteLine(a(1)) ' Index outside of bounds error here



Ken
---------------------
Herfried and Cor:-

I used tracing and actually tracked down the code that was causing the
problem most likely. I wonder if you wanted to comment on it.

Also I wonder if there is a better way of testing if there is data than
testing the length of the xml string I used as stringreader to create the
dataset, but thats a side issue.

I think I tried isdbnull and is nothing and stuff like that and they cause
run time errors if there isnt any data, in other areas of code I have had
problems with using it so found this a reasonable way of ensuring the string
returned with data that must mean there was data - as I have a catch anyway
so if any errors occur, it will quit before it gets to these string length
testing parts of code.

Anyway back to the actual problem. I THINK its because the application
usually throws a comma in the field1, but I can reproduce the error if I
don't.
I THINK I either need to change my selection of fields (and get less
complicated and get data from seperate fields that might contain same data )
or test for an index of comma in the file, and do something different if
there is no comma.

If Len(xmlstr1) > 25 Then
For intCounter = 0 To dataset1.Tables("result").Rows.Count - 1
dt.Rows.Add(CreateRow("[" +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(1).subs
tring(1, 1).toupper +
dataset1.Tables("result").Rows(intCounter).Item("field1").split(",")(0).subs
tring(0, 1).toupper + "]:" +
dataset1.Tables("result").Rows(intCounter).Item("field1"),
dataset1.Tables("result").Rows(intCounter).Item("field2"), dt))
Next
End If


"An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the exception
can be identified using the exception stack trace below.

Stack Trace:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
System.Array.InternalGetValue(Int32 index1, Int32 index2, Int32 index3)
+0
System.Array.GetValue(Int32 index) +32
Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet(Object o,
Object[] args, String[] paramnames) +187
sharedcal.ShareCal.o() +655
sharedcal.ShareCal.Page_Load(Object o, EventArgs e) +799
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top