|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Getting Exception on Command Line Args, but works within IDE debuginput, 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. 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. 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. 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. 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? I also just tried using:
My.Application.CommandLineArgs as a method of accessing the arguments and I get the exact same error. "David B" <thedavidbow***@yahoo.com> wrote in message My first thought would be that maybe args(0) is the program name (it is 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. under Unix) which may be different when running in the IDE. 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. "David B" <thedavidbow***@yahoo.com> wrote in message If the IL has a debugger (does it?) you could step through the code - since 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. it's the first bit it shouldn't be too hard. "David B" <thedavidbow***@yahoo.com> wrote in message Whynews: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. 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 David B wrote:
> Unhandled Exception: System.IndexOutOfRangeException: Index was outside Your command line arguments are NOT the only array you're playing with!> 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. > 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 " _ would fail, big time.> & "(ID, TESTVAL1, TESTVAL2) VALUES " _ > & "(" & LineArray(0) _ > & ", '" & LineArray(1) & "'" _ > & ", '" & LineArray(2) & "'" _ > & ")" 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.
Multi-line comments
Communication between forms VS2005 - Passing NumericUpDown into a class method? Legacy Connection Object - I need a new one Execute something stored in a variable Editing Multiple DataGridViews IE Privacy - DNS Problem - ASP.NET security issues with an upgraded VB6 program DX9 SDK (Feb 2006) / .NET 2.0 Creating an inherited control |
|||||||||||||||||||||||