Click here to Skip to main content
15,877,051 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to import text file using VB.NET and importing it into an EXCEL FILE.

But I am having errors with my code:
- index was outside the bounds of the array
- Index was out of range. Must be non-negative and less than the size of the collection parameter name:index

Private Sub Datacalculate(ByVal files() As String)
       Try

           Dim ti() As String = New String(100000) {}
           For division As Integer = 0 To 4
               For ctr As Integer = 0 To files.Count - 1
                   Dim testarray() As String = Split(files(ctr), ",", )
                   ti(ctr) = testarray(0)
                   GetValuesPerChannel(testarray(division), division)
               Next
           Next

       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
   End Sub



I cant figure out what the problem is.
The excel shows only 2 columns. and the rest was blank.
Posted
Updated 8-Sep-14 21:11pm
v4
Comments
ChauhanAjay 8-Sep-14 22:53pm    
Did you debug to find out about the error?
On which line do you receive this error?
NekoNao 8-Sep-14 22:55pm    
I did debug it .the error is in this codes. Its like it is not reading the THIRD COLUMN


GetValuesPerChannel(testarray(division), division)


xlWorkSheet.Cells(i + 2, 4) = Column3(i).ToString
//Third Column
MuhammadUSman1 8-Sep-14 23:29pm    
You are accessing an object or index which does not exists yet.
MuhammadUSman1 8-Sep-14 23:31pm    
It is "GetValuesPerChannel" built in function or you created this function?
NekoNao 8-Sep-14 23:48pm    
I created the getvaluesperchannel..

Put a breakpoint within DataCalculate and go step by step, find option "step into" in your debugger (it is F11 if you're using Visual Studio)...
Dim testarray() As String = Split(files(ctr), ",", )
ti(ctr) = testarray(0)

Not sure why you have empty space behind second comma in the split, but you're definitely accesing testarray (0) without checking if it has elements (it should have the first, but then you're following with
GetValuesPerChannel(testarray(division), division) 

This part is by no means guaranteed: - check the length of testarray after split.
testarray(division) <-- you probably have a file with less then division commas.
 
Share this answer
 
v2
Comments
NekoNao 9-Sep-14 2:25am    
Aw. Too Technical. Can you explain it more ? Cause I am just new in programming
Sinisa Hajnal 9-Sep-14 2:32am    
Being new does not excuse you from not trying. You should always strive to find your own solution reading the forums like this one and adapting, rather then waiting for the finished code which you will copy without understanding. Also, whatever IDE you're using, learn how to follow the code with breakpoints and how to do step by step checking of the code. Never use objects without checking their validity.

SOLUTION
If testarray.Length <= 5 (or whatever number of columns you're expecting) Then
MsgBox("The file does not contain information about all the columns!")
Return
End If
(you can decorate messagebox with various styles like info or warning if you want)
NekoNao 9-Sep-14 2:34am    
I did try many codes and try to debug it. but since i have lack of knowldege. I really cant see the solution. Only i know is the logic not the code. Its been my 1 week in debugging this code still dont find the right code.
Sinisa Hajnal 9-Sep-14 2:46am    
"Knowing" will get you stuck. Re-check your assumptions, check the file as I mentioned. Check the length of the testarray (You have the code in the post above), check that all files have same structure etc...
NekoNao 9-Sep-14 2:36am    
Guess the file does not contain the exact number . But Why do you think is that if in the TEXTFILE, it is complete.
The only solution here is to NEVER RENAME .txt file into a .log file.
MUST CREATE A NEW .LOG FILE ^__________^
Because i think it can't read it.

OR

CREATE A NEW TEXT FILE.

I tried using the REAL .log and it works!

Thanks for all the people who commented. ^^
 
Share this answer
 
v2
Comments
Sinisa Hajnal 9-Sep-14 3:15am    
Then you have something in your program that specifically reads log file of the given format. Or you had other txt files in the folder.
I've used the code like yours for xml, txt, csv and dat files and everything works fine with those files.

No problem, glad you solved it.
NekoNao 9-Sep-14 3:21am    
Its maybe because of the format of the files INSIDE the textfile is not the same as the .log file

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