title: Decoding HTML and databinding into a ASP.NET repeater
body: <p>I have a separate page where I use the Server.HTMLEncode feature to encode HTML a user has entered inside of a HTMLEditorExtender on a TextBox. </p>
  
  <p>I am trying to insert this HTML into a repeater like so:</p>
  
  <pre><code>&lt;asp:Repeater id="articleList" runat="server"&gt;
      &lt;ItemTemplate&gt;
          &lt;div class="itemtemplate"&gt;
  
          &lt;h2&gt;&lt;%#Container.DataItem("Title")%&gt;&lt;/h2&gt;
  
          &lt;h5&gt;Category:&lt;/h5&gt; &lt;%#Container.DataItem("Category")%&gt;&lt;br /&gt;
          &lt;%#Container.DataItem("decodedHTML")%&gt;
          &lt;%#Container.DataItem("UserName")%&gt;
          &lt;%#Container.DataItem("DateOfPost")%&gt;
      &lt;/div&gt;
      &lt;/ItemTemplate&gt;
      &lt;AlternatingItemTemplate&gt;
      &lt;div class="altitemtemplate"&gt;
       &lt;h2&gt;&lt;%#Container.DataItem("Title")%&gt;&lt;/h2&gt;
  
          &lt;h5&gt;Category:&lt;/h5&gt; &lt;%#Container.DataItem("Category")%&gt;&lt;br /&gt;
          &lt;%#Container.DataItem("decodedHTML")%&gt;
          &lt;%#Container.DataItem("UserName")%&gt;
          &lt;%#Container.DataItem("DateOfPost")%&gt;
      &lt;/div&gt;
      &lt;/AlternatingItemTemplate&gt;
      &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>And my code behind:</p>
  
  <pre><code>Sub displayArticles()
      Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
      conn.Open()
      Dim cmd As New OleDbCommand("SELECT * FROM [UserArticles] ORDER BY DateOfPost DESC", conn)
      Dim inputString As String = "HTMLBody"
      Dim decodedHTML As String = Server.HtmlDecode(inputString)
      articleList.DataSource = cmd.ExecuteReader()
      articleList.DataBind()
      conn.Close()
  End Sub
  </code></pre>
  
  <p>"HTMLBody" is the name of the field in my database with the encoded HTML in.</p>
  
  <p>Unfortunately, I am receiving the error </p>
  
  <pre><code>"IndexOutOfRangeException was unhandled by user code".
  </code></pre>
  
  <p>There is obviously a problem here referring to the string decodedHTML in my Container.DataItem statement, so what am I doing wrong?</p>
  
  <p>EDIT: code from the other page where the html is encoded:</p>
  
  <pre><code>    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
          If String.IsNullOrEmpty(TextBox1.Text) Then
          ErrorMessage.Visible = True
          ErrorMessage.Text = "Your submission is blank. Please write your article first"
          Else
              Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
              Dim SqlString As String = "Insert into UserArticles(Title,Category,UserName,DateOfPost,HTMLPost) Values (@f1,@f2,@f3,@f4,@f5)"
              Dim HTMLEncode As String = Server.HtmlEncode(TextBox1.Text)
              Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
              cmd.CommandType = CommandType.Text
              cmd.Parameters.AddWithValue("@f1", ArticleTitle.Text)
              cmd.Parameters.AddWithValue("@f2", CategoryDropDown.SelectedValue)
              cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
              cmd.Parameters.AddWithValue("@f4", DateTime.Now.Date)
              cmd.Parameters.AddWithValue("@f5", HTMLEncode)
              oleDbConn.Open()
              cmd.ExecuteNonQuery()
              TextBox1.Text = Nothing
              ArticleTitle.Text = Nothing
              CategoryDropDown.ClearSelection()
          End If
  End Sub
  </code></pre>