Click here to Skip to main content
15,886,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this is my code but when I click on search there's an error! Please help..
thanks

<pre lang="vb">


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from AMORLITO where _name = '" & TextBox1.Text & "'", con) 'Change items to your database name

If da.Fill(ds, "AMORLITO") Then 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView1.DataSource = view
Else
MsgBox("NO DATA!")
End If
End Sub
Posted
Updated 30-Jan-20 9:15am
Comments
Kaushik S Murthy 24-Apr-15 9:04am    
What is the error that you are getting?
Richard Deeming 24-Apr-15 9:12am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Member 11566968 24-Apr-15 9:14am    
Syntax error in query expression '_name = '''
its my first time in programming. ^^,
Member 11566968 24-Apr-15 9:15am    
what are the examples of parameterized query?Im a beginners thanks
Member 11566968 24-Apr-15 9:31am    
thank you so much, how can i concatenate name?

1 solution

Start by fixing the SQL Injection[^] vulnerability in your code.

Then, check your column name. Is it really called _name (with a leading underscore character)? If so, you'll need to wrap it in square brackets.

Try something like this:
VB
Dim da As New OleDbDataAdapter("SELECT * FROM AMORLITO WHERE [_name] = ?", con)

' OleDb doesn't use named parameters, so the parameter name doesn't matter here:
da.SelectCommand.Parameters.AddWithValue("p0", TextBox1.Text)

Dim ds As New DataSet()
If da.Fill(ds, "AMORLITO") Then
    Dim view As DataView = ds.Tables(0).DefaultView
    source1.DataSource = view
    DataGridView1.DataSource = view
Else
    MsgBox("NO DATA!")
End If
 
Share this answer
 
Comments
Sascha Lefèvre 24-Apr-15 9:31am    
+5

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