title: How to escape apostrophe when displaying inside a ASP.NET label
body: <p>I have the following function which truncates a SQL Server <code>varchar</code> column and adds it to a string:</p>
  
  <pre><code>public void Pp()
  {
      strSql = @""; //query
      using (SqlConnection conn = new SqlConnection(gstr))
      {
          try
          {
              SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
  
              myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              string specific = "";
              string generic = "";
              string strTemp = "";
  
              foreach (DataRow r in myDataSet.Tables[0].Rows)
              {
                  if (r["MessageText"].ToString().Length &lt;= 65)
                  {
                      strTemp = r["MessageText"].ToString();
                  }
                  else
                  {
                      strTemp = TruncateLongString(r["MessageText"].ToString(), 65);
                  }
                  specific += "&lt;/b&gt;&lt;span class='hoverText' title='" + r["MessageText"] + "'&gt;" + strTemp + "...&lt;/span&gt;";
                  strTemp = "";
              }
  
              lblMessage.Text = "&lt;b&gt;SPECIFIC MESSAGES:&lt;/b&gt; &lt;br /&gt;" + specific;
          }
          catch (Exception ce)
          {
          }
      }
  }
  public string TruncateLongString(string str, int maxLength)
  {
      return str.Substring(0, maxLength);
  }
  </code></pre>
  
  <p>If the <code>r["MessageText"]</code> contains an appostrophe, it cuts off anything after it. (full text: <code>no way no way HERE we go again but this is not working. Is it? or Is it not? I can't be too sure though. Can someone please check.</code>)</p>
  
  <p>Here is an example of a live preview (the <code>title</code> is shown but gets cut off because of the apostrophe):</p>
  
  <p><img src="https://i.stack.imgur.com/GcqAT.png" alt="enter image description here"></p>
  
  <p>Here is the source (the apostrophe is shown in the purple box. Also the color coding gets out of whack due to the apostrophe, which means the code is not correct):</p>
  
  <p><img src="https://i.stack.imgur.com/H9zng.png" alt="enter image description here"></p>
  
  <p>How can I ensure it doesn't escape any escape characters, e.g. <code>'</code>, <code>/</code>, <code>\</code>.</p>