Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
String sql = "select PatientId, FirstName, LastName, Sex, Age, CNIC, Phone, GaurdianName from Patients WHERE 1=1";
            if (PatientId.Text != "")
            {
                
               sql += " AND PatientId LIKE '%" + PatientId.Text + "%'";
            }

i couldn't uderstand where is wrong!! i want to retrieve exact patient id but not getting like if i query for 1 it is showing 1,10,11 records!! i couldn't fighure out where is the problem.
Posted
Updated 1-Jun-13 18:05pm
v2
Comments
[no name] 1-Jun-13 23:53pm    
"1 it is showing 1,10,11 records" and so it should. Do you have any idea what LIKE does? That is the output that I would expect. If you want the exact PatientId then you just need to use =
David_Wimbley 2-Jun-13 0:05am    
Should make that the answer, would have my 5
Faraz the fighter 2-Jun-13 0:52am    
sql += " AND PatientId == + PatientId.Text + ";

not giving the required result
bbirajdar 2-Jun-13 2:00am    
== should be = since it is sql syntax
debkumar@codeproject 2-Jun-13 0:07am    
What is the use of 1=1? Unnecessarily adding clause. I believe query optimizer removes this from the query.

'LIKE' and '%' are used for finding elements based on substrig (ignoring case sensitivity). '=' is used for finding exact (ignoring case sensitivity).

To add to what the others have said, don't do it like that anyway.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
To get exact match, your query should looks like:
SQL
SELECT PatientId, FirstName, LastName, Sex, Age, CNIC, Phone, GaurdianName
FROM Patients
WHERE PatientId LIKE =@PatientId


I would suggest you to create stored procedure and call it from code behind.
Using a Stored Procedure with Output Parameters[^]
How to create a SQL Server stored procedure with parameters [^]
 
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