views:

61

answers:

4

I am accessing data from an SQLServer database to my ASP.Net C# based website. Now this data that i'm accessing is recieved from affiliate companies and it contains "& amp;#163;" (without quotes and space) for the £ sign. But when I'm displaying this data inside a label, instead of £ it shows "& #163;" (without quotes and space again ofcourse).

Note: The & amp; is converted to &.

I know it's a character encoding problem and I've tried to provide the character encoding as 'utf-8' and also 'ISO-8859-1' in the header.

Which character encoding should I use to solve the problem.

Any help is deeply appreciated. Thanks.

+1  A: 

&#163 should render as &#163. By using the & you've escaped the HTML escape code.

Kirk Woll
probably yes, that's the case, but do I have a solution to solve the problem? :\I surely cant change the data in the table, so I would have to find a solution at the application end.
Anchit
It sounds like it's been HTML-encoded twice. Therefore, perhaps you can just HTML-decode it twice? (HttpUtility.HtmlDecode)
Kirk Woll
But the label is in the itemtemplate of a gridview, here it goes:<ItemTemplate><asp:Label ID="Label8" runat="server" Text='<%# Bind("Gift") %>'> </asp:Label></ItemTemplate>So how can i apply htmldecode to it? :/
Anchit
Lots of ways. Remember that what sits between the <%# and %> tags is just C#. So, you could have: Text='<%# HttpUtility.HtmlDecode(Bind("Gift")) %>'
Kirk Woll
+1  A: 

It appears to be encoded twice and decoded only once. What was the step it goes through ?

James Curran
A: 

might seem stupid, but does the font your using has this symbol?

steven
erm, yes it does :)Trebuchet MS.
Anchit
+1  A: 

It's not a problem with finding the correct character encoding to turn the bytes into text, it's how the text has been encoded (or rather escaped).

The problem is that the text has been HTML encoded twice. The first time it turns the £ into &#163;, the second time into &amp;#163;.

So, to correct this you have to use the HtmlDecode method to reverse the second step.

Guffa
thanks, i'll try it.
Anchit
But, the label which is displaying the data is in the item template of a gridview: <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Gift") %>'></asp:Label> </ItemTemplate>so how can I put HtmlDecode function there?Sorry for asking novice questions I am just a beginner.
Anchit
You can't bind the property if you want to change the value before displaying it, use `Eval` instead: `Text='<%# HttpUtility.HtmlDecode(Eval("Gift")) %>'`
Guffa
That was giving the error of invalid arguments for HtmlDecode, so tried this:Text='<%# HttpUtility.HtmlDecode(Eval("Gift").ToString()) %>'And it worked like a charm, but all thanks to you for providing me the right direction. Really appreciate your help :)
Anchit