Search code examples
c#asp.nettextboxapostrophe

apostrophes turning into ' when I get text from textbox


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?


Solution

  • 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;