Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
I have a c# code from which an parameterize SELECT store procedure is being called. One of the parameter is the name of a person. It is encoded with HTML. But when I am trying to search some details of a person whose name has an ' character it is not returning any results.

The reason is the ' character is encoded to '
Example: These is a name in database called O'nail. When I am trying to search it gets htmlencrypted to O'nail. and it couldn't be found from database.

It will be a help if any one gives me an work around.
Posted
Comments
Herman<T>.Instance 13-Mar-15 8:48am    
Do you use Server.HtmlEncode/HtmlDecode methods in asp.net page?
See: http://www.dotnetperls.com/htmlencode-htmldecode.

Since you parameterize the SP the encoding/decoding can be skipped
Sergey Alexandrovich Kryukov 13-Mar-15 9:02am    
SQL injection is unrelated to HTML encoding. It's not clear why data is HTML-encoded in first place. Normal HTML controls' values won't HTML-encode anything. So it depends on what you do. If some data is HTML-encoded, decode it before the search. How can it be a problem?
—SA
Herman<T>.Instance 13-Mar-15 9:20am    
I think the person uses HtmlEncoding to prevent sql injection because some characters will be ranslatred then. One of them is '. But he uses parameterized sp, so the encoding is not needed.
Sergey Alexandrovich Kryukov 13-Mar-15 9:27am    
Ah... maybe, but it would be quite silly. And you are right about parametrized statements...
—SA
Sinisa Hajnal 13-Mar-15 10:02am    
Apostrophe (') serves in the databases as string mark (as in 'This is a string')...if you look for it in the data, you should replace it with two apostrophes ('')

DECLARE @a varchar(20)
SET @a = ' O''neal'

SELECT * FROM table wHERE name like @ + '%'

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