473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Apr 20 '06 #1
10 2211
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.

Apr 20 '06 #2
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.

Apr 20 '06 #3
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.

Apr 20 '06 #4
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?

Apr 20 '06 #5
I also just tried using:

My.Application.CommandLineArgs

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

Apr 20 '06 #6

"David B" <th************@yahoo.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.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.


Apr 20 '06 #7
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.

Apr 20 '06 #8

"David B" <th************@yahoo.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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.


Apr 21 '06 #9

"David B" <th************@yahoo.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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

Apr 21 '06 #10
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.
Apr 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lekyan | last post by:
I have problem setting the password for an ADAM user using C#. I used the SetPassword code given in the Microsoft page, changed several parameters, but ran into an exception. I wonder if other...
2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
3
by: Robert Brinson | last post by:
I'm writing one of my first WinForm apps; everything else has been WebForms up to this point. However, having come from a background of Java, C++, and Perl, I'm having trouble understanding how...
4
by: Brian Henry | last post by:
I get the following exception only on one system (dev system) and not on any others... what could cause this?! I can access the SMTP server just fine in E-Mail applications on the machine... ...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
8
by: Cybert | last post by:
I just installed Microsoft Visual Basic .NET and wrote a very simple program that references a .tlb file called "Wilbur". The first time I ran the program below everything worked fine. But...
6
by: tvaughan77 | last post by:
Hi, I have some code that I want to use to run a command line utility and I want to be able to run it from an aspx page running under IIS. The command line utility is a local utility running on...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
3
by: Robert Rawlins | last post by:
Hi Mk, Yeah it's got me a little bemused to be honest, I've tried playing around with configuration options this morning and not been able to achieve anything that works properly. I'll keep...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.