title: How can I display multiple images from my DB inside a grid-view under one Post ID?
body: <p>I am working on a blog site. Current functionality allows a user to upload a photo (using a fileUpload control) as a part of their post (each post has a unique Post ID attached to it). However the user can only upload one photo per post. I am trying to make it so that they can upload multiple images and have all of them seen in a single post within the gridview. The user would CTRL+Click the images they want uploaded after hitting "browse' on the fileUpload control. I was successfully able to insert each of the selected images into the DB under one Post ID in this situation using the code below. </p>
  
  <pre><code>   if (imageUpload.HasFiles)
      {
          System.Text.StringBuilder sb = new System.Text.StringBuilder();
          HttpPostedFile file;
  
          SqlCommand maxMessId = new SqlCommand("SELECT Max(MessageID) FROM BlogMessages", conn);
          lastMessageID = Convert.ToInt32(maxMessId.ExecuteScalar());
  
          foreach (var uploadedFile in imageUpload.PostedFiles)
          {
              file = uploadedFile; 
              MemoryStream ms = new MemoryStream();
              file.InputStream.CopyTo(ms);
              byte[] byts = ms.ToArray();
              ms.Dispose();
  
              SqlCommand cmdInsertImage = new SqlCommand("INSERT INTO BlogImages(Image, MessageID) VALUES (@Image, @MessageID)", conn);
              cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = byts;
              cmdInsertImage.Parameters.AddWithValue("@MessageID", lastMessageID);
              cmdInsertImage.ExecuteNonQuery();
              byts = null;
              cmdInsertImage = null; 
  
  
          }
  
      }
  </code></pre>
  
  <p>So the first half of what I am trying to do is working, but what I am having trouble with is getting all of the uploaded images to display on the post once the 'Post' button is clicked. Currently it will display only one of the images, the first in the list of selected images within the fileUpload. My current code as part of my imageHandler class:</p>
  
  <pre><code>try
  {
      int messageid = Convert.ToInt32(context.Request.QueryString["mid"]);
  
      //SqlCommand command = new SqlCommand("SELECT Image from BlogMessages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn);
  
      SqlDataAdapter sda = new SqlDataAdapter("SELECT Image from BlogImages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn);
      DataTable data = new DataTable();
      sda.Fill(data);
  
  
      if (data != null)
      {
          for (int i=0; i &lt; data.Rows.Count; i++)
          {
  
  
              Byte[] bytes = (Byte[])data.Rows[i]["Image"];
              context.Response.Buffer = true;
              context.Response.Charset = "";
              //context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
              //context.Response.AddHeader("content-disposition", "attachment;filename="
              //+ data.Rows[0]["Name"].ToString());
              context.Response.BinaryWrite(bytes);
  
          }
              context.Response.Flush();
              context.Response.End();
      }
  }
  </code></pre>
  
  <p>Any suggestions on how I can go about displaying ALL of the images that were inserted into the DB for a post? Thank you!</p>
  
  <p>Gridview Definition:</p>
  
  <pre><code>&lt;asp:GridView ID="gvPosts" runat="server" AutoGenerateColumns="False" DataKeyNames="MessageID" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Width="693px"
          OnPageIndexChanging="gvPosts_PageIndexChanging" OnRowEditing="gvPosts_RowEditing"
          OnRowCancelingEdit="gvPosts_RowCancelingEdit" OnRowUpdating="gvPosts_RowUpdating" AllowPaging="True"&gt;
          &lt;AlternatingRowStyle BackColor="#CCCCCC" /&gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="Posts" HeaderStyle-Font-Size="Larger"&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="tbGridTitle" runat="server" Text='&lt;%# Bind("Title") %&gt;' Font-Bold="true" Font-Size="Large" ForeColor="#cc0000"&gt;&lt;/asp:TextBox&gt;
                      &lt;div style="margin-left: 20px"&gt;
                          &lt;p&gt;
                              &lt;asp:TextBox ID="tbGridAuthor" runat="server" Text='&lt;%# Bind("Author") %&gt;' Font-Bold="true" Font-Size="Small" Width="150px"&gt;&lt;/asp:TextBox&gt;
                              &lt;asp:Label ID="lblDate" runat="server" Text='&lt;%# Bind("Date","{0:MM/dd/yyyy}") %&gt;' Font-Bold="true" Font-Size="Small" Width="100px"&gt;&lt;/asp:Label&gt;
                              &lt;asp:Label ID="lblEditIPAddress" runat="server" Text='&lt;%# Bind("IPAddress") %&gt;' Font-Bold="true" Font-Size="Small" Width="100px"&gt;&lt;/asp:Label&gt;
                              &lt;%--&lt;asp:Label ID="lblMID" runat="server" Text='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID") %&gt;'&gt;&lt;/asp:Label&gt;--%&gt;
                          &lt;/p&gt;
                      &lt;/div&gt;
                      &lt;div style="margin-left: 40px"&gt;
                          &lt;asp:TextBox ID="tbGridMessage" runat="server" Text='&lt;%# Bind("Message") %&gt;' TextMode="MultiLine" Width="450px" Height="200px"&gt;&lt;/asp:TextBox&gt;
                          &lt;div&gt;
                              &lt;asp:Image ID="postImage" runat="server" ImageUrl='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID") %&gt;' Width="400px" AlternateText="No Image" ImageAlign="Middle" /&gt;
                              &lt;asp:Image ID="Image1" runat="server" ImageUrl='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID") %&gt;' Width="400px" AlternateText="No Image" ImageAlign="Middle" /&gt;
                           &lt;/div&gt; 
  
  
                      &lt;/div&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="Label4" runat="server" Text='&lt;%# Bind("Title") %&gt;' Font-Bold="true" Font-Size="Large" ForeColor="#cc0000"&gt;&lt;/asp:Label&gt;
                      &lt;div style="margin-left: 20px"&gt;
                          &lt;p&gt;
                              &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("Author") %&gt;' Font-Bold="true" Font-Size="Small" Width="150px"&gt;&lt;/asp:Label&gt;
                              &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Bind("Date","{0:MM/dd/yyyy}") %&gt;' Font-Bold="true" Font-Size="Small" Width="100px"&gt;&lt;/asp:Label&gt;
                              &lt;asp:Label ID="lblGridIPAddress" runat="server" Text='&lt;%# Bind("IPAddress") %&gt;' Font-Bold="true" Font-Size="Small" Width="100px" Visible="false"&gt;&lt;/asp:Label&gt;
                              &lt;%--&lt;asp:Label ID="lblMID" runat="server" Text='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID") %&gt;'&gt;&lt;/asp:Label&gt;--%&gt;
                          &lt;/p&gt;
                      &lt;/div&gt;
                      &lt;div style="margin-left: 40px"&gt;
                          &lt;asp:Label ID="Label3" runat="server" Text='&lt;%# Bind("Message") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;div&gt;
                              &lt;%--&lt;asp:Image ID="postImage" runat="server" ImageUrl='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID") %&gt;' Width="400px" AlternateText="No Image" ImageAlign="Middle" Visible="false" /&gt;--%&gt;
                              &lt;asp:Image ID="postImage" runat="server" ImageUrl='&lt;%# "ImageHandler.ashx?mid="+ Eval("MessageID")%&gt;' Width="400px" AlternateText="No Image" ImageAlign="Middle" Visible="false" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;HeaderStyle Font-Size="Large" /&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton runat="server" Text="View Comments" OnClick="lbViewComments_Click"&gt;&lt;/asp:LinkButton&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="MessageID" SortExpression="MessageID" Visible="false"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblMessageID" runat="server" Text='&lt;%# Bind("MessageID") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField ShowHeader="False" Visible="False"&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt;
                      &amp;nbsp;&lt;asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"&gt;&lt;/asp:LinkButton&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField ShowHeader="False" Visible="False"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClick="lbDelete_Click"&gt;&lt;/asp:LinkButton&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
          &lt;FooterStyle BackColor="#CCCCCC" /&gt;
          &lt;HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /&gt;
          &lt;PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /&gt;
          &lt;SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#808080" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#383838" /&gt;
      &lt;/asp:GridView&gt;
  </code></pre>