Retrieve the data using LIKE % (SQL query) in C#

Posted by Agopi.net under C# on 11/12/2010 | Points: 10 | Views : 31090 | Status : [Member] | Replies : 5
Hi Team,

Retrieve the data from database into datagridview using SQL query (Like %) in C# 2008. while I perform the below coding in C# it's not work. Below is my coding.

SQLconnection con=new SQLconnection();
SQLCommand cmd=new SQLCommand();
SQLDataadapter adp=new SQLDataadapter();

con=new sqlconnection(" .........connection string here ..........");
con.open();

adp=new sqldataadapter(" select customername from customers where customername LIKE % (' "+ textbox1.text+" ')",con);

ds=new dataset();

adp.fill(ds);

datagridview1.datasource=ds.datatable[0];


con.close();

It's not work. What is a wrong here? Please help me.




Responses

Posted by: Madhuk on: 11/12/2010 [Member] Starter | Points: 25

Up
0
Down
chnage your query
(" select customername from customers where customername LIKE % (' "+ textbox1.text+" ')"

to
(" select customername from customers where (customername LIKE ' %  "+ textbox1.text+" ') "


Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 11/22/2010 [Member] Starter | Points: 25

Up
0
Down
Hi Madhuk,

After i changed the query, it cannot Retrieve the data from database. it visible only table name. Could you please check and tell the fault.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhuk on: 11/22/2010 [Member] Starter | Points: 25

Up
0
Down
Make sure that the customername you are entering in textbox is present in the db.
select customername from customers where (customername LIKE ' %  "+ textbox1.text+" '

for ex: you have gopi in your db. The above statement is true when textbox value is gopi
select customername from customers where (customername LIKE ' %  "+ textbox1.text+" %' 

when text box matches any character of gopi it is true.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gokul on: 12/8/2010 [Member] Starter | Points: 25

Up
0
Down
You place a breakpoint and check what value is entererd in the textbox1. If the textbox1 value matches the value in DB then surely the query will execute.

Thanks and Regards,
GokulNath Nithy.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: adityaguru91-21038 on: 7/10/2013 [Member] Starter | Points: 25

Up
0
Down
select childname from registrations where childname like '" + nametxt.Text + "%'"

aditya

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response