Avatar of Member_2_1242703
Member_2_1242703

asked on 

Using wildcard to query Access DB in ASP.NET(VB)

I am binding a datagrid to an MS Access DB table. It works fine except when I use a query that involves a wildcard search. I've tried several methods, none of which seem to work. I've even tried the same query within access and it works, but not in .NET.
Here's my query...

select TargetCompany, ID, NextActionDate, NextAction from tblMain where TCProducts like '*" & strKey & "*' or PCType like '*" & strKey & "*' or PCNotes like '*" & strKey & "*' or NextAction like '*" & strKey & "*' or Notes like '*" & strKey & "*' ORDER BY TargetCompany


Like I said, I've tried moving the quotes/asterisks around every way imaginable, and I've tried using = instead of like. I don't get any errors, just not data. Can someone please show me the proper format for writing a query using a wildcard character when connected to an Access DB table through and ASP.NET page (VB)

Here's my vb code also...

    Sub BindData()

Dim strKey As String = Label8.Text
Label1.Text = "select TargetCompany, ID, NextActionDate, NextAction from tblMain where TCProducts like '*" & strKey & "*' or PCType like '*" & strKey & "*' or PCNotes like '*" & strKey & "*' or NextAction like '*" & strKey & "*' or Notes like '*" & strKey & "*' ORDER BY TargetCompany"

    Dim ds As New Data.DataSet
        Dim ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.MapPath("contacts.mdb")
        Dim da As New Data.OleDb.OleDbDataAdapter(Label1.Text, ConString)
        da.Fill(ds, "employees")
        GridView1.DataSource = ds.Tables("employees")
        GridView1.DataBind()
    End Sub
ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
alfredwhang
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You have to use % and _ as wildcards even though you are querying access. When you use any ADO or ADO.NET methods to connect to an access database the wildcards *MUST* be % and _ not * and ?:

Label1.Text = "select TargetCompany, ID, NextActionDate, NextAction from tblMain where TCProducts like '%" & strKey & "%' or PCType like '%" & strKey & "%' or PCNotes like '%" & strKey & "%' or NextAction like '%" & strKey & "%' or Notes like '%" & strKey & "%' ORDER BY TargetCompany"
Avatar of YZlat
YZlat
Flag of United States of America image

select TargetCompany, ID, NextActionDate, NextAction from tblMain where TCProducts like '%" & strKey & "%' or PCType like '%" & strKey & "%' or PCNotes like '%" & strKey & "%' or NextAction like '%" & strKey & "%' or Notes like '%" & strKey & "%' ORDER BY TargetCompany

Sub BindData()

Dim strKey As String = Label8.Text
Label1.Text = "select TargetCompany, ID, NextActionDate, NextAction from tblMain where TCProducts like '%" & strKey & "%' or PCType like '%" & strKey & "%' or PCNotes like '%" & strKey & "%' or NextAction like '%" & strKey & "%' or Notes like '%" & strKey & "%' ORDER BY TargetCompany"

    Dim ds As New Data.DataSet
        Dim ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.MapPath("contacts.mdb")
        Dim da As New Data.OleDb.OleDbDataAdapter(Label1.Text, ConString)
        da.Fill(ds, "employees")
        GridView1.DataSource = ds.Tables("employees")
        GridView1.DataBind()
    End Sub
Avatar of alfredwhang
alfredwhang

you are using Jet so the wildcard character is % and not *
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo