title: asp:repeater w/ asp:image and generic-handler - SQLConnection crash
body: <p>I have an asp repeater which sets a generichandler (.ashx) parameter differently for each entry. This is used to create comments that are posted by users and display the avatar of the user posting by fetching it from a database. If there is only one post, it works fine, but as soon as there are two or more posts I get a crash in the "GetIdFromUserName" method on the line "connection.Open()" (code provided below) saying "The connection was not closed, the connections current state is open". Bearing in mind this only happens when more than one comment exists, I have no idea what causes this. </p>
  
  <p>One thing I have found out, though: if I make the asp:image object not use the "Eval" (so it only shows the same image for every post regardless of ) it works, but beyond that I have no idea. Eval does work fine if there's only one post to show, though, like mentioned earlier.</p>
  
  <p>If anyone have any idea how to fix this I'd be super grateful.</p>
  
  <p>ASP code:</p>
  
  <pre><code>        &lt;asp:Repeater ID="CommentsRepeater" runat="server" OnItemDataBound="CommentsRepeater_ItemDataBound"&gt;
              &lt;ItemTemplate&gt;
                  &lt;div class="comment"&gt;
                      &lt;div class="commentInfo"&gt;
                          &lt;strong&gt;
                              &lt;asp:Image ID="Avatar" ImageUrl='&lt;%# "CommentsAvatarHandler.ashx?id=" + Eval("UserName") %&gt;' runat="server" Height="100px" Width="100" /&gt;
                              &lt;asp:LinkButton ID="CommentName" Text='&lt;%# Eval("UserName") %&gt;' CommandArgument='&lt;%# Eval("UserId") %&gt;' CausesValidation="false" runat="server"&gt;&lt;/asp:LinkButton&gt;&lt;/strong&gt; @ 
                          &lt;asp:Label ID="CommentDate" Text='&lt;%# Eval("PostedDate") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                          &lt;div class="commentVote"&gt;
                              &lt;asp:LinkButton ID="VoteUp" CommandArgument='&lt;%# Eval("CommentId") %&gt;' OnClick="VoteUpClick" CausesValidation="false" runat="server"&gt;&lt;img src="images/pluss2.png" /&gt;&lt;/asp:LinkButton&gt;
                              &lt;asp:LinkButton ID="VoteDown" CommandArgument='&lt;%# Eval("CommentId") %&gt;' OnClick="VoteDownClick" CausesValidation="false" runat="server"&gt;&lt;img src="images/minus2.png" /&gt;&lt;/asp:LinkButton&gt;
                              &lt;strong&gt;(&lt;asp:Label ID="CommentVote" Text='&lt;%# Eval("Vote") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;)&lt;/strong&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="commentText"&gt;
                          &lt;asp:Label ID="CommentText" Text='&lt;%# Eval("Comment") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/ItemTemplate&gt;
              &lt;SeparatorTemplate&gt;
                  &lt;br /&gt;
              &lt;/SeparatorTemplate&gt;
          &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Relevant bit: </p>
  
  <pre><code>   &lt;asp:Image ID="Avatar" ImageUrl='&lt;%# "CommentsAvatarHandler.ashx?id=" + Eval("UserName") %&gt;' runat="server" Height="100px" Width="100" /&gt;
  </code></pre>
  
  <p>Here's the handler: </p>
  
  <pre><code>public class CommentsAvatarHandler : IHttpHandler
  {
  
      public void ProcessRequest(HttpContext context)
      {
          String userName = context.Request.QueryString["id"];
          Gamez.Sql.GetAvatarOfUser(context, userName);
      }
  
      public bool IsReusable
      {
          get
          {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>the method used by the handler:</p>
  
  <pre><code>    public static void GetAvatarOfUser(HttpContext context, String userName)
      {
          SqlDataReader reader;
  
          try
          {
              Guid id = GetIdFromUserName(userName);
              command.CommandText = "SELECT Avatar FROM UserAttributes WHERE UserId = @id";
              command.Parameters.AddWithValue("@id", id);
  
              connection.Open();
              reader = command.ExecuteReader();
              while (reader.Read())
              {
                  try
                  {
                      context.Response.ContentType = "image/jpg";
                      context.Response.BinaryWrite((byte[])reader["Avatar"]);
                  }
                  catch { }
              }
              if (reader != null)
                  reader.Close();
  
          }
          finally
          {
              if (connection != null)
              {
                  connection.Close();
              }
          }
  
      }
  </code></pre>
  
  <p>The GetIdFromUserName method:</p>
  
  <pre><code>    public static Guid GetIdFromUserName(String name)
      {
          command.Parameters.Clear();
          command.CommandText = "SELECT UserId FROM aspnet_Users WHERE UserName = @UserName";
          command.Parameters.AddWithValue("@UserName", name);
          connection.Open();  // &lt;---- DESCRIBED CRASH HAPPENS HERE
          Guid id = (Guid)command.ExecuteScalar();
          connection.Close();
          return id;
      }
  </code></pre>
  
  <p>GetAvatarFromUser and GetIdFromUserName are in the same library and have access to the same SQLCommand (command) and SQLConnection (connection) objects.</p>