I have one problem, i have sql database in this table one colume is having special character, if am tring to display the data its giving error
like "Incorrect syntax error 's'."
For example data like this "Allari Naresh's 'ARRJJ' In Tamil "
its raising this type of error "
Incorrect syntax near 'ARRJJ'.
"so please help to me to avoid this kind of errors.
Am using listview control to display the whole data.
Upamanyu Roy ChoudhuryPosted Feb 23, 2013, 2:02 AM
From UI
Here is a method, that takes an input string and a list of characters that should not be encoded.
public static string HtmlEncode(string input, params string[] except) {
var encodedInput = HttpUtility.HtmlEncode(input);
foreach (var character in except) {
var encodedCharacter = HttpUtility.HtmlEncode(character);
encodedInput = encodedInput.Replace(encodedCharacter, character);
}
return encodedInput;
}And the call:
var output = HtmlEncode("test ®", "®");
Now have a look at the decode method
public static string DecodeSpecialHtmlCode(string input) {
var specials = new[]{"©", "™", "&right;"}; // Add more special codes
foreach (var scharacter in specials) {
var decodedCharacter = HttpUtility.HtmlDecode(scharacter);
input = input.Replace(scharacter, decodedCharacter);
}
return input;
}And the call:
var output = DecodeSpecialHtmlCode("Microsoft ®"); // output -> Microsoft ®
output = HttpUtility.HtmlEncode(output);
// output -> Microsoft ® <Visual Studio>and browser will display: Microsoft ®
Following two articles will help you to do this
http://www.dotnetperls.com/htmlencode-htmldecode
http://www.dotnetperls.com/httputility
Look at the following links to have a broader concept
http://stackoverflow.com/questions/1562360/converting-amp-to-etc
http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma
SQL Server End
Do something like this
execute sp_executesql
N'SELECT * from employee e where e.NAME LIKE ''%'' + @searchName + ''%''',
N'@searchName nvarchar(255)',
@searchName = Allari Naresh's 'ARRJJ' In Tamil
It will help you to avoid confusing quote escaping, but it also protects you against Sql Injection attacks.
Also read the following link to have a clear concept
http://sqlserver2000.databases.aspfaq.com/how-do-i-search-for-special-characters-e-g-in-sql-server.html
Please mark it as an answer if this posts is of any help to you
Keep me posted
Anoop KPosted Feb 21, 2013, 9:23 AM