J
JohnT
I fell like a Dufuss...
Going from VB6 to VB.net and it's like walking all over again... I think I'm
at the crawl point
I'm using VB.net and opening a MS Access db. Then displaying the info into a
Data Grid. So far I can manage to do that.
What I'm trying to do now is to have two Command Line arguments and to be able
to use Wild Cards to pre-select the info to display based upon the Command
Line Arguments.
I thought I had it correct from all the examples I've seen. I've tested the
Command Line arguments and they work Fine. I've tested my code to open the
data base and display the DB into the Data Grid. Again, works fine. I just
fall flat on the Command Line and Fill.
My db PrimaryKey is the First item (User_Name).
Here is what I have so far:
Imports System.Data
Imports System.Data.OleDb
Dim UsrName As String ' User Name
Dim UsrAge As String ' User Age
' Declare an internal variable to hold the Command Line Arguments
Dim CommandLineArgs As String()
' Add to the Main() method to call Application.Run() to launch the class
Shared Sub Main(ByVal args As String())
Application.Run(New Form1(args))
End Sub
' Modify the New() method so that it accepts the Command Line Arguments
Public Sub New(ByVal args As String())
MyBase.New()
CommandLineArgs = args
Dim c As Process = Process.GetCurrentProcess()
'This call is required by the Windows Form Designer
InitializeComponent()
End Sub
.... command line arguments are:
MyApplicationName.exe "David Browne" "32"
.... now I Open my DB, etc....
Dim strgConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\MyDBtest.mdb"
Dim OleDbConn As OleDbConnection = New OleDbConnection
OleDbConn.ConnectionString = strgConn
UsrName = UCase(CommandLineArgs(0)) 'Job Number
UsrAge = UCase(CommandLineArgs(1)) 'Bill Code
' Data Adapter
Dim dbStrSel As String
dbStrSel = "Select User_Name, User_Number," & _
"User_Age, User_Age from UserTable"
Dim daUsers As New OleDbDataAdapter
daUsers = New OleDbDataAdapter(dbStrSel, OleDbConn)
Dim dsUsers As DataSet = New DataSet
daUsers.Fill(dsUsers, "UserTable")
' Attach DataSet to DataGrid
DataGrid1.DataMember = "UserTable"
The Above code works! The whole db file tables and rows are listed Now if I
change the DataAdapter String to include the WHERE statement thus:
' Data Adapter
Dim dbStrSel As String
dbStrSel = "Select User_Name, User_Number," & _
"User_Age, User_Age from UserTable " & _
"WHERE (User_Name LIKE ?) AND (User_Age LIKE ?)"
And I then add the following Before the FILL:
' Select Data Based upon User Info
daUsers.SelectCommand.Parameters(0).Value = UsrName
daUsers.SelectCommand.Parameters(1).Value = UsrAge
.... Now I get an Error on the First SelectCommand.Parameter line...
Additional information: Invalid index 0 for this OleDbParameterCollection with
Count=0.
Obviously (but not to me) there is some piece of the puzzle I am missing here?
Index 0???
I look up OleDbParameterCollection and I go cross eyed in wonder!!! I'd
appreciate some help! I'm sure the reason can't be very complexed (I hope).
Many Thanks!
JohnT
Going from VB6 to VB.net and it's like walking all over again... I think I'm
at the crawl point
I'm using VB.net and opening a MS Access db. Then displaying the info into a
Data Grid. So far I can manage to do that.
What I'm trying to do now is to have two Command Line arguments and to be able
to use Wild Cards to pre-select the info to display based upon the Command
Line Arguments.
I thought I had it correct from all the examples I've seen. I've tested the
Command Line arguments and they work Fine. I've tested my code to open the
data base and display the DB into the Data Grid. Again, works fine. I just
fall flat on the Command Line and Fill.
My db PrimaryKey is the First item (User_Name).
Here is what I have so far:
Imports System.Data
Imports System.Data.OleDb
Dim UsrName As String ' User Name
Dim UsrAge As String ' User Age
' Declare an internal variable to hold the Command Line Arguments
Dim CommandLineArgs As String()
' Add to the Main() method to call Application.Run() to launch the class
Shared Sub Main(ByVal args As String())
Application.Run(New Form1(args))
End Sub
' Modify the New() method so that it accepts the Command Line Arguments
Public Sub New(ByVal args As String())
MyBase.New()
CommandLineArgs = args
Dim c As Process = Process.GetCurrentProcess()
'This call is required by the Windows Form Designer
InitializeComponent()
End Sub
.... command line arguments are:
MyApplicationName.exe "David Browne" "32"
.... now I Open my DB, etc....
Dim strgConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\MyDBtest.mdb"
Dim OleDbConn As OleDbConnection = New OleDbConnection
OleDbConn.ConnectionString = strgConn
UsrName = UCase(CommandLineArgs(0)) 'Job Number
UsrAge = UCase(CommandLineArgs(1)) 'Bill Code
' Data Adapter
Dim dbStrSel As String
dbStrSel = "Select User_Name, User_Number," & _
"User_Age, User_Age from UserTable"
Dim daUsers As New OleDbDataAdapter
daUsers = New OleDbDataAdapter(dbStrSel, OleDbConn)
Dim dsUsers As DataSet = New DataSet
daUsers.Fill(dsUsers, "UserTable")
' Attach DataSet to DataGrid
DataGrid1.DataMember = "UserTable"
The Above code works! The whole db file tables and rows are listed Now if I
change the DataAdapter String to include the WHERE statement thus:
' Data Adapter
Dim dbStrSel As String
dbStrSel = "Select User_Name, User_Number," & _
"User_Age, User_Age from UserTable " & _
"WHERE (User_Name LIKE ?) AND (User_Age LIKE ?)"
And I then add the following Before the FILL:
' Select Data Based upon User Info
daUsers.SelectCommand.Parameters(0).Value = UsrName
daUsers.SelectCommand.Parameters(1).Value = UsrAge
.... Now I get an Error on the First SelectCommand.Parameter line...
Additional information: Invalid index 0 for this OleDbParameterCollection with
Count=0.
Obviously (but not to me) there is some piece of the puzzle I am missing here?
Index 0???
I look up OleDbParameterCollection and I go cross eyed in wonder!!! I'd
appreciate some help! I'm sure the reason can't be very complexed (I hope).
Many Thanks!
JohnT