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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David B

    #1

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

    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.IndexOut OfRangeExceptio n: 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 ConnectionStrin g As String = "Data
    Source=localhos t;Integrated Security=SSPI;I nitial Catalog=DEVTEST "
    Dim Con As New SqlConnection(C onnectionString )
    Dim CmdInsert As New SqlCommand("INS ERT 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.OpenSta ndardOutput()
    Console.WriteLi ne(args(0))


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

    Con.Open()

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

    Cmd.CommandType = CommandType.Tex t
    Cmd.ExecuteNonQ uery()

    Con.Close()

    Loop

    End Sub

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

    Thanks,

    David B.

  • Sam

    #2
    Re: Getting Exception on Command Line Args, but works within IDE debug

    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.

    Comment

    • Sam

      #3
      Re: Getting Exception on Command Line Args, but works within IDE debug

      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.

      Comment

      • Sam

        #4
        Re: Getting Exception on Command Line Args, but works within IDE debug

        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.

        Comment

        • David B

          #5
          Re: Getting Exception on Command Line Args, but works within IDE debug

          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?

          Comment

          • David B

            #6
            Re: Getting Exception on Command Line Args, but works within IDE debug

            I also just tried using:

            My.Application. CommandLineArgs

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

            Comment

            • Homer J Simpson

              #7
              Re: Getting Exception on Command Line Args, but works within IDE debug


              "David B" <thedavidbowles @yahoo.com> wrote in message
              news:1145548433 .628758.287840@ j33g2000cwa.goo glegroups.com.. .
              [color=blue]
              > 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.[/color]

              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.




              Comment

              • David B

                #8
                Re: Getting Exception on Command Line Args, but works within IDE debug

                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.

                Comment

                • Homer J Simpson

                  #9
                  Re: Getting Exception on Command Line Args, but works within IDE debug


                  "David B" <thedavidbowles @yahoo.com> wrote in message
                  news:1145557329 .627374.298370@ i39g2000cwa.goo glegroups.com.. .[color=blue]
                  > 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.[/color]

                  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.




                  Comment

                  • Homer J Simpson

                    #10
                    Re: Getting Exception on Command Line Args, but works within IDE debug


                    "David B" <thedavidbowles @yahoo.com> wrote in message
                    news:1145557329 .627374.298370@ i39g2000cwa.goo glegroups.com.. .[color=blue]
                    > 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.[/color]

                    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(ar gument)
                    Console.WriteLi ne(argument)
                    Next



                    Comment

                    • Phill W.

                      #11
                      Re: Getting Exception on Command Line Args, but works within IDEdebug

                      David B wrote:
                      [color=blue]
                      > Unhandled Exception: System.IndexOut OfRangeExceptio n: 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.[/color]

                      Your command line arguments are NOT the only array you're playing with!
                      [color=blue]
                      > LineArray = Split(LineOfTex t, ",")[/color]

                      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
                      [color=blue]
                      > Dim BatchQuery As String = "INSERT INTO DEVTEST " _
                      > & "(ID, TESTVAL1, TESTVAL2) VALUES " _
                      > & "(" & LineArray(0) _
                      > & ", '" & LineArray(1) & "'" _
                      > & ", '" & LineArray(2) & "'" _
                      > & ")"[/color]

                      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(ar gs(0))
                      Dim sText As String _
                      = sr.ReadLine()
                      Do While Not (sr Is Nothing)
                      AllText = AllText & LineOfText & vbCrLf
                      Dim LineArray As String() _
                      = LineOfText.Spli t(","c)

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

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

                      HTH,
                      Phill W.

                      Comment

                      Working...