Wildcars with DataAdapter

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
 
J

Jay Pondy

If I were searching in Access for LastNames starting with "P" the
where statement would be: LastName like 'P*'

so you might try:

WHERE (User_Name LIKE '?*') AND (User_Age LIKE '?*')

or skip the parameters and load the SQL statement directly with:

sSQL = string.format( "... where (User_Name like '{0}*' and (User_Age
like '{1}*'", UsrName, UsrAge)

The ParamValues will be substitued for {0} and {1} placeholders
resepectively.
 
J

JohnT

so you might try:
WHERE (User_Name LIKE '?*') AND (User_Age LIKE '?*')

or skip the parameters and load the SQL statement directly with:

sSQL = string.format( "... where (User_Name like '{0}*' and (User_Age
like '{1}*'", UsrName, UsrAge)

The ParamValues will be substitued for {0} and {1} placeholders
resepectively.

Interestingly neither worked :(

Would it make ANY sense if I add the following lines right After I DIM the
Data set?

objDA.SelectCommand.Parameters.Add("@Job_Number", OleDbType.BSTR)
objDA.SelectCommand.Parameters.Add("@Bill_Code", OleDbType.BSTR)

I only have 2 lines of info in my Test DB... because I am getting possible
'correct' results. I just don't know IF what I am doing is correct (or just
damn close).

Thanks again

JohnT
 
G

Guest

Sorry John it was late last night - here's what your looking for:

Dim oCon As OleDbConnection = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=MyAccessDB.mdb")

Dim oCmd As OleDbCommand = New OleDbCommand("select * from UserTable where
UserName like @UserName", oCon)

Dim oAdp As OleDbDataAdapter = New OleDbDataAdapter(oCmd)
Dim oTbl As DataTable = New DataTable

Try
oCmd.Parameters.Add("@UserName", "j%")
oAdp.Fill(oTbl)
MessageBox.Show(String.Format("Found {0} rows", oTbl.Rows.Count))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
If oCon.State = ConnectionState.Open Then oCon.Close()
End Try
 
G

Guest

John

Create an Access Query named qryUserNameLike and add a text parameter named
UserNameIndex.

In the Query Criteria add:

Like [UserNameIndex] & "%"

Note that the Query will NOT work in Access unless you change the wild card
to "*".

Here's how to call the Query from ADO.Net 1.1
============================================

Dim oCon As OleDbConnection = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=MyAccessDB.mdb")
Dim oCmd As OleDbCommand = New OleDbCommand("qryUserNameLike", oCon)
Dim oAdp As OleDbDataAdapter = New OleDbDataAdapter(oCmd)
Dim oTbl As DataTable = New DataTable

Try
oCmd.CommandType = CommandType.StoredProcedure
oCmd.Parameters.Add("@UserNameIndex", "j")
oAdp.Fill(oTbl)
MessageBox.Show(String.Format("Found {0} rows", oTbl.Rows.Count))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
If oCon.State = ConnectionState.Open Then oCon.Close()
End Try

============================================
And your right going for VB6 to Dot Net is a pretty tough climb. After 4
years I still feel overwhelmed.

One of the best books around on ADO.Net is David Sceppa's "ADO.Net Core
Reference" http://www.microsoft.com/MSPress/books/authors/auth5354.asp
I highly recommend it.
 
J

JohnT

Note that the Query will NOT work in Access unless you change the wild card
to "*".

Here's how to call the Query from ADO.Net 1.1
============================================

Okay... I'll take a look at your samples... many thanks!
One of the best books around on ADO.Net is David Sceppa's "ADO.Net Core
Reference" http://www.microsoft.com/MSPress/books/authors/auth5354.asp
I highly recommend it.

I actually have this book! :)

But for most part, it is Way Over my head. I don't program VB.net for 'work'.
More 'pleasure' (if I can use that word - hahaha). So it's not an every day
kind of thing I work on. Thus I take longer to learn things (and forget them
even faster). Oh well...

Thanks again for your time. I'll struggle onward and Hopefully figure out
what you've written (as it applies to my needs).

JohnT
 
P

Paul Clement

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

Take a look at the following documentation. It uses a Command object.

http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.selectcommand.aspx

In the future you may want to post ADO.NET questions to:

microsoft.public.dotnet.framework.adonet


Paul
~~~~
Microsoft MVP (Visual Basic)
 
C

Cor Ligthert [MVP]

Paul,

This happens to me as well sometimes.
In the future you may want to post ADO.NET questions to:

microsoft.public.dotnet.framework.adonet
It was crossposted, I was not visiting the ado newsgroup.

:)

Cor
 
P

Paul Clement

¤ Paul,
¤
¤ This happens to me as well sometimes.
¤
¤ > In the future you may want to post ADO.NET questions to:
¤ >
¤ > microsoft.public.dotnet.framework.adonet
¤ >
¤ It was crossposted, I was not visiting the ado newsgroup.

Cor,

Let me rephrase:

In the future you may want to limit ADO.NET questions to:

microsoft.public.dotnet.framework.adonet

How's that? ;-)


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top