Take the 2-minute tour ×

I am facing a strange problem that my service would not return any results, if I search for a product name (eg: ABC 123 XYZ) by pasting the string in to the search text box in my winform app.

The user follows this mode and faces the problem to search: A table with product names in outlook - copied in to excel - and then a string from that table in excel is copied in to my application's search text box to search.

The same string when I type it in manually(ABC 123 XYZ) in to my search box, works fine and service returns the result. Hence I suspected some encoding issue when the value is copied from excel in to my app's text box.

I did some reading on encoding and found this tool. http://www.codeproject.com/Articles/17201/Detect-Encoding-for-In-and-Outgoing-Text

When I copy the text from excel - it shows 'Western European(Windows), iso-8859-1.

When I type it in - it shows US-ASCII. So, I concluded that it is better to convert my text to US-ASCII always.

Text encoding when typed in to text box directly

enter image description here

Text encoding when I copied from an excel cell

Text encoding when I copied from an excel cell

I followed the below code to convert it to US-ASCII and find this to work. The problem seems to be in the 'space' between the letters and hence in the below code, the EncoderReplacementFallback is " ".

string inputString = textBox1.Text;
Encoding encoder = ASCIIEncoding.GetEncoding("us-ascii", 
                   new EncoderReplacementFallback(" "), 
                   new DecoderExceptionFallback());
byte[] bAsciiString = encoder.GetBytes(inputString);
string cleanString = new ASCIIEncoding().GetString(bAsciiString);

This code makes it work though.

I am not sure if this approach is right to convert any string pasted or typed in to the search box. Will this work for all cases irrespective of what encoding the pasted text is or when typing by hands ?


share|improve this question
I doubt that will work, if you type text in that would otherwise be viewed as "western European" ASCII might not have all the characters you need. Maybe if you explain more what you're trying to accomplish and less about what you're doing, someone might be able to offer some advice. e.g. what makes you think converting to ASCII will make this WCF service work better? –  Peter Ritchie Oct 11 '12 at 13:56
Thanks. I understand your point but thats what I am not sure of. But this code actually works whereas if I pass in the original string (which shows up to be in a diff encoding in the utility) the service doesnot return any data. –  Everything Matters Oct 11 '12 at 14:48
A string like "ABC 123 XYZ" has the same encoding in any code page. It is very unclear what your problem might be, other than that the example string you gave is not actually one you are having trouble with. –  Hans Passant Oct 11 '12 at 14:53
@Mani If textBox1.Text == "ABC 123 XYZ" is false, they are not the same strings. It's unclear from the detail you've provided what the problem is. –  Peter Ritchie Oct 11 '12 at 15:23
What are those glyphs between C and 1 and 3 and X. that's an indication that what you've pasted isn't ASCII... –  Peter Ritchie Oct 11 '12 at 15:50

Your Answer



Browse other questions tagged or ask your own question.