Home All Groups Group Topic Archive Search About

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

Author
20 Apr 2006 3:53 PM
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.

Author
20 Apr 2006 4:56 PM
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.
Author
20 Apr 2006 4:56 PM
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.
Author
20 Apr 2006 4:56 PM
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.
Author
20 Apr 2006 5:07 PM
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?
Author
20 Apr 2006 5:20 PM
David B
I also just tried using:

My.Application.CommandLineArgs

as a method of accessing the arguments and I get the exact same error.
Author
20 Apr 2006 6:05 PM
Homer J Simpson
"David B" <thedavidbow***@yahoo.com> wrote in message
news:1145548433.628758.287840@j33g2000cwa.googlegroups.com...

> 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.
Author
20 Apr 2006 6:22 PM
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.
Author
20 Apr 2006 11:56 PM
Homer J Simpson
"David B" <thedavidbow***@yahoo.com> wrote in message
news:1145557329.627374.298370@i39g2000cwa.googlegroups.com...
> 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.
Author
21 Apr 2006 12:09 AM
Homer J Simpson
"David B" <thedavidbow***@yahoo.com> wrote in message
news:1145557329.627374.298370@i39g2000cwa.googlegroups.com...
> 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
Author
21 Apr 2006 11:49 AM
Phill W.
David B wrote:

> 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.