Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to display record word wise.but problem in fire query in vb.net
This is my sql query

SQL
SELECT * FROM emp.dbo.di1
        'WHERE city LIKE 's%'



and i making the following query

SQL
cmd = New SqlCommand("select * from Di1 Where Word LIKE" & "'" & TextBox7.Text & "'" & "%" & "", cn)
        adp = New SqlDataAdapter(cmd)
        adp.Fill(ds, "Di1")
        DataGridView1.DataSource = ds.Tables("Di1")


the above code display syntax error near % sign

and also grid view control does not update using above code.

pls correct my query and grid view
Posted
Updated 24-Mar-11 17:15pm
v2

First, use parameterized queries to execute your SQL statement, not this string concatenation you're using. Why? You have a problem that is EASILY avoided by doing do.

This is what you're query currently looks like:
select * from Di1 Where Word LIKE'somevalue'%

You're missing a space after LIKE and the '%' character needs to be INSIDE the single quote marks, not outside of it.
 
Share this answer
 
Your code should be like this:


VB
cmd = New SqlCommand("select * from Di1 Where Word LIKE" & "'" & TextBox7.Text & "%'",cn)
        adp = New SqlDataAdapter(cmd)
        adp.Fill(ds, "Di1")
        DataGridView1.DataSource = ds.Tables("Di1")


your upper query is correct but in the 2nd piece of code you have 's'% instead of 's%'.

'%' includes within single qoute.
 
Share this answer
 
v2

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