Getting Exception on Command Line Args, but works within IDE debug

D

David B

I am writing a console App with VB 2005 where I accept a text file as
input, parse it and insert the text data into an SQL table. I have
written the code correctly as well as I can tell because it works
running it within the IDE, given that you have to go into the Debug
Project Properties to specify the argument. If I build the file and go
run it from the command line, specifying the argument, I get the
following error:

C:\>devtest.exe devtest.txt
devtest.txt

Unhandled Exception: System.IndexOutOfRangeException: Index was outside
the boun
ds of the array.
at DEVTEST.Module1.Main(String[] args)

Note, for debugging purposes, I write to the console the first element
of the args() array. I don't see how the index is outside the bounds
of the array especially when it works fine within the IDE. If anyone
has any ideas, any suggestions would be helpful. I am writing and
running this from VB 2005 Express. Below is my full code:

***********************************************************************************************************
Module Module1

Dim LineOfText As String
Dim AllText As String
Dim LineArray(3) As String
Private ConnectionString As String = "Data
Source=localhost;Integrated Security=SSPI;Initial Catalog=DEVTEST"
Dim Con As New SqlConnection(ConnectionString)
Dim CmdInsert As New SqlCommand("INSERT INTO DEVTEST (ID, PO_NAME,
AMOUNT) VALUES ('123', 'TEST', '123.32')", Con)
Dim args(2) As String


Public Sub Main(ByVal args() As String)

Console.OpenStandardOutput()
Console.WriteLine(args(0))


FileOpen(1, args(0), OpenMode.Input, OpenAccess.Read,
OpenShare.Default)
Do Until EOF(1)
LineOfText = LineInput(1)
AllText = AllText & LineOfText & vbCrLf
LineArray = Split(LineOfText, ",")

Con.Open()

Dim BatchQuery As String = "INSERT INTO DEVTEST (ID,
TESTVAL1, TESTVAL2) VALUES (" & LineArray(0) & ", '" & LineArray(1) &
"' , '" & LineArray(2) & "')"
Dim Cmd As New SqlCommand(BatchQuery, Con)
Dim Adapter As New SqlDataAdapter(Cmd)

Cmd.CommandType = CommandType.Text
Cmd.ExecuteNonQuery()

Con.Close()

Loop

End Sub

End Module
************************************************************************************************************

Thanks,

David B.
 
S

Sam

Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename 'devtest' and start
adding '.txt' within the program.

That is the easiest solution I can think of.
 
S

Sam

Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename 'devtest' and start
adding '.txt' within the program.

That is the easiest solution I can think of.
 
S

Sam

Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename 'devtest' and start
adding '.txt' within the program.

That is the easiest solution I can think of.
 
D

David B

I hardcoded the .txt to be appended onto the args(0) with:

args(0) & ".txt"

and that accomplished what is should have via running it in the IDE but
once I executed it at the console, I got the same results as I pasted
above. Any other ideas?
 
D

David B

I also just tried using:

My.Application.CommandLineArgs

as a method of accessing the arguments and I get the exact same error.
 
H

Homer J Simpson

Note, for debugging purposes, I write to the console the first element
of the args() array. I don't see how the index is outside the bounds
of the array especially when it works fine within the IDE.

My first thought would be that maybe args(0) is the program name (it is
under Unix) which may be different when running in the IDE.
 
D

David B

I just checked your suggestion and you are right. args(0) is the
executed filename, however due to the fact that I have the executable
and the text file named the same thing, all it was doing was taking the
filename and throwing on an extension of '.txt' which is what the text
file is called. So I am still getting the same error message. I'm
beginning to wonder if this isn't some sort of bug within VB because I
have checked and rechecked and nothing is out of the ordinary. Like I
said under the IDE it works fine but it doesn't not work when I try the
executable and pass it arguments from the command line.
 
H

Homer J Simpson

David B said:
I just checked your suggestion and you are right. args(0) is the
executed filename, however due to the fact that I have the executable
and the text file named the same thing, all it was doing was taking the
filename and throwing on an extension of '.txt' which is what the text
file is called. So I am still getting the same error message. I'm
beginning to wonder if this isn't some sort of bug within VB because I
have checked and rechecked and nothing is out of the ordinary. Like I
said under the IDE it works fine but it doesn't not work when I try the
executable and pass it arguments from the command line.

If the IL has a debugger (does it?) you could step through the code - since
it's the first bit it shouldn't be too hard.
 
H

Homer J Simpson

David B said:
I just checked your suggestion and you are right. args(0) is the
executed filename, however due to the fact that I have the executable
and the text file named the same thing, all it was doing was taking the
filename and throwing on an extension of '.txt' which is what the text
file is called. So I am still getting the same error message.

Why

Dim args(2) As String

That doesn't look kosher. This works OK

For Each argument As String In My.Application.CommandLineArgs
' Add code here to use the argument.
'Debug.Print(argument)
Console.WriteLine(argument)
Next
 
P

Phill W.

David said:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside
the bounds of the array.
at DEVTEST.Module1.Main(String[] args)

Note, for debugging purposes, I write to the console the first element
of the args() array. I don't see how the index is outside the bounds
of the array especially when it works fine within the IDE.

Your command line arguments are NOT the only array you're playing with!
LineArray = Split(LineOfText, ",")

If a line in your text file does not have the requisite number of commas
(for example "X,Y", or even a BLANK line) you would wind up with /less/
items in the array than you expect, so the statement
Dim BatchQuery As String = "INSERT INTO DEVTEST " _
& "(ID, TESTVAL1, TESTVAL2) VALUES " _
& "(" & LineArray(0) _
& ", '" & LineArray(1) & "'" _
& ", '" & LineArray(2) & "'" _
& ")"

would fail, big time.

Always assume that anything coming from Outside your program is tainted
and code defensively, something like (VB2003-biased air-code):

Dim sr as New StreamReader(args(0))
Dim sText As String _
= sr.ReadLine()
Do While Not (sr Is Nothing)
AllText = AllText & LineOfText & vbCrLf
Dim LineArray As String() _
= LineOfText.Split(","c)

If LineArray.GetUpperBound(0) >= 2 Then
Con.Open()
...
Con.Close()
End If

' Next Line
LineOfText = sr.ReadLine()
Loop
sr.Close()

HTH,
Phill W.
 
Top