Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim connection As OleDb.OleDbConnection = New OleDbConnection
       connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\InventSystem.accdb"
       connection.Open()
       Dim da As OleDb.OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Items WHERE Barcode Like  '%" & TextBox1.Text & "%' ", connection)
       Dim ds As New DataTable
       da.Fill(ds)
       If ds.Rows.Count = 0 Then
           MessageBox.Show("Record not Found")
       End If
       connection.Dispose()


What I have tried:

I'm really new to this. Can you guys help me i feel like there is missing on my code to filter the datagrid
Posted
Updated 26-Feb-17 3:58am
Comments
Richard Deeming 24-Feb-17 12:15pm    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Using connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|AppData|\InventSystem.accdb")
    Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Items WHERE Barcode Like '%' + @find + '%'", connection)
    da.SelectCommand.Parameters.AddWithValue("@find", TextBox1.Text)
    
    Dim table As New DataTable()
    da.Fill(table)
    
    If table.Rows.Count = 0 Then
        MessageBox.Show("Record not found")
    Else
        ' TODO: Show the returned rows somewhere...
    End If
End Using
Richard Deeming 24-Feb-17 12:15pm    
Beyond that, you haven't actually told us what the problem is.

1 solution

I'd suggest to use Google[^].

As Richard Deeming[^] mentioned you have to use parameterized query (OleDbCommand[^]), for example:
VB.NET
Dim qry As String = "SELECT * FROM Items WHERE Barcode Like '*@find*';"


Note: MS Access database engine is using [*] instead of [%]. Please see: Like Operator (Microsoft Access SQL) [Access 2007 Developer Reference][^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900