Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm getting this error "index was outside the bounds of the array". Here my code
Dim totalcolumn As Integer
If grdTraining.Columns.Count > totalcount Then
   totalcolumn = grdTraining.Columns.Count - totalcount
   For i As Integer = 0 To totalcolumn
      Dim field1 As HyperLinkField = DirectCast(Me.grdTraining.Columns(j), HyperLinkField)
      field1.DataTextField = ColumnsArray(i)
      Me.grdTraining.Columns(j).Visible = False
      j = j + 1
End If
BindGridView(grdTraining, dtTrainings)
Updated 7-Apr-15 23:48pm
DamithSL 8-Apr-15 2:09am
FOR loop with i but inside you are increment j, where you define j? and what is the initial value?
SathishRam 8-Apr-15 2:16am
thanks for your answer,j initialized before in my code ,J starts with value 6,when j=12 and i=6 i'm getting code break
Sinisa Hajnal 8-Apr-15 2:18am
Does your grdTraining has 21 columns? Also, commonly you go from 0 to columnCount-1...because of zero index start, your last column will have index one less than columnCount.
SathishRam 8-Apr-15 2:25am
grdTraining columns is 15
Sinisa Hajnal 8-Apr-15 2:16am
One of the easiest bugs to resolve. Put a breakpoint at the start of the block and step line-by-line until you find exactly where it fails. Check all values. In the code above there are couple of potential culprits:
grdTraining.Columns(j), ColumnsArray(i) - you're not showing ColumnsArray or j initialiation so its hard to say for sure. Learn to debug, this is not forum question material as it is very easy to resolve.
No, don't go step by step. Just run it, the application will stop at exception (if exception options are correctly set up, which is the case by default). Then put a break point one line before this point, repeat, proceed by steps.
SathishRam 8-Apr-15 2:19am
At beak point ,J starts with value 6,when j=12 and i=6 i'm getting code break
Sinisa Hajnal 8-Apr-15 4:38am
Excellent. Now walk through the code and find out which of the arrays runs out of indices. How many elements are there in ColumnsArray? How many columns the gridview has?

1 solution

debug your code line by line and try to rectified the problem,try to find which array runs out of indices.
Check how many columns in column array and how many columns gridview really has. :)
deepankarbhatnagar 8-Apr-15 5:30am
You are suggesting him so please use comment for this.
upendra shahi 8-Apr-15 6:26am
I'm suggesting him because at-least he should try to do hard work for his issue.
Agree. Please see my other comment to this answer. Anyway, the vote of 1 someone gave you was hardly fair, I up-voted it.
upendra shahi 9-Apr-15 1:26am
thanx sir..
I think this advice is still acceptable as an answer, maybe not detailed enough, but really leading the inquirer for some resolution. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

Doesn't it make sense?

deepankarbhatnagar 8-Apr-15 6:28am
Thats good upendra but you have to write that thing on the same panel where you are asnwering me. :) simple
upendra shahi 8-Apr-15 7:17am
thanx sir..
deepankarbhatnagar 8-Apr-15 8:56am
got the answer..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900