Pregunta

So I understand that apostrophes will screw up when you try to insert them into a database, but my apostrophes screw up before that even! Here is what my code looks like:

   String test = null;
   test= Textbox1.Text.ToString();

When I go through and debug this, and say I put in "How's it going" into the textbox1, I see that when i pass that value onto test, the apostrophe in How's turns into "&#39".

I'm guessing it has something to do with the encoding of the page? But I'm not really sure. Does anyone know? And is there a list of chars that will screw up like this so I can make a universal fix?

¿Fue útil?

Solución

Try using HttpUtility.HtmlDecode. It seems like you are getting an encoded string.

StringWriter myWriter = new StringWriter();
HttpUtility.HtmlDecode(Textbox1.Text.ToString(), myWriter);
String test = myWriter;

Otros consejos

I was facing same issue. I had solved it by changing content type of page.

<%@ Page Language="C#" ContentType="application/xhtml+xm" %>

I think you have two options

  1. Replace your special characters like Single Quote with Say...Single_Quote and while retrieving from the database you can convert back to Single Quote while rendering

  2. Insert the string as it is in the database. Now, while fetching from the database you can use Literal Control. This control provides three properties

    (a) Transform

    (b) Encode

    (c) Transform

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top