Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string query="Select * from Products where CategoryID='" + Request.QueryString["ID"] + "' Or ProductID ='"+Request.QueryString["ID"]+"' or ProductName='"+Request.QueryString["Name"]+"'";



i am using the above query in productcatalog.aspx.

i am using this part (
C#
ProductName='"+Request.QueryString["Name"]+"'"
) to show the results of search using the a textbox entry and a button in some other page..

but i want to show the results by using like in sql query

like
C#
ProductName like '"+Request.QueryString["Name"]+"%'"


so if i write the query in this way:

C#
string query="Select * from Products where CategoryID='" + Request.QueryString["ID"] + "' Or CategoryID='" + Request.QueryString["ID"] + "' or ProductName LIKE '"+Request.QueryString["Name"]+"%'"


the result of the search will be correct but the other two Querystrings e.g
C#
CategoryID='" + Request.QueryString["ID"] + "'
and
C#
CategoryID='" + Request.QueryString["ID"] + "'
in sql query will not give desired results
Posted

If CategoryID in your database is an integer, this won't work:

CategoryID='" + Request.QueryString["ID"] + "'


You won't need the single quotes.

CategoryID=" + Request.QueryString["ID"] + "


But seriously, the way you're building your query is SQL injection waiting to happen:

http://en.wikipedia.org/wiki/SQL_injection[^]

I'd seriously look at parameterizing your queries.
 
Share this answer
 
Please, please don't do it like this. Your code is screaming for a sql injection attack.
Read the Querystring arguments into separate variable and validate that the values fall in an acceptable range and apply defaults to them if they do not exist in the querystring. Then, once the variables are considered valid create your sql query variable from the sub components. Never, ever apply non validated user input into a query directly.
 
Share this answer
 
You can always debug your code, copy this query to the backend and then run it there.
It will help you get to the exact root of the problem.
 
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