Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i have a query which gets data when i enter an information but i want that query to get data when i enter a part of data like 1 or more alphabets...i tried using LIKE % but it showed error...i am posting my query below...can anybodu help me refine it?


select * from Agent where AgentId= '" + txtA_ID.Text.ToString() + "' or RegistrationDate= '" + txtReg_Date.Text.ToString() + "' or Email = '" + txtEmail.Text.ToString() + "' or  Phone = '" + txtPhone.Text.ToString() + "' or AccountType = '" + txtAct_Type.Text.ToString() + "' or CreditCardNum = '" + txtCredit_Card.Text.ToString() + "' or CardExpiration = '" + txtCardExp.Text.ToString() + "' or Domain = '" + txtDomain.Text.ToString() + "'"
Posted
Comments
ZeeroC00l 3-Apr-11 1:20am    
I cant see your query that has LIKE % used. Could you paste here that query too so that we can look into as to what is wrong.

And since you are using '' and using text box to get details from the user, i don't thing .ToString()function is necessary.
Laksh112 3-Apr-11 17:12pm    
hey ya i did not put the code with like % here coz when ever i use Like% it gives an error saying an error near the " (the " next to Like %)

A couple of things:
1) Your query as written will returns rows where any one of the conditions passes. I.e. if the user has entered the ID, or the user has entered the Reg_Date, or... You need to think about this a bit more.
2) You do not need to use the ToString method on Text properties: they are already strings!
3) You should not create queries by concatenating strings: it leave you wide open to an accidental or deliberate SQL Injection attack, which could destroy your database. Instead, use parametrized queries:
SqlCommand com = new SqlCommand("SELECT * FROM Agent WHERE AgentID=@ID OR RegistrationDate=@RD", con);
com.Parameters.AddWithValue("@ID", txtA_ID.Text); 
com.Parameters.AddWithValue("@ID", txtReg_Date.Text);
(I shortened it, but you get the idea).

I would suggest you start small: use LIKE on a single field, and then expand as needed
 
Share this answer
 
Comments
Laksh112 3-Apr-11 15:58pm    
Thank you.I will try doing what you said...
Use string pattern to get you desire alphabets...
 
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