title: Saving multiple Images at once to the database with a FileUpload Control
body: <p>I am working on a company Blog site, and when a user is making a post, they can add an image from their computer to the post. I used a FileUpload control to do this, and it works great. However, I am trying to change the functionality to allow the user to select and upload multiple images in one post, and I am running into some issues. I have set the 'allow multiple' property to 'true', however once multiple images are selected into the control (Image URLs get separated by a comma) and the post button is clicked, only one of the images is inserted into the database, but it is inserted as many times as images there are, and only that one image is displayed on the blog post. So if I try to add three different images, it inserts three instances of the first image into the database. The code corresponding to the the FileUpload in my onClick function is below:</p>
  
  <pre><code>  if (imageUpload.HasFile == true)
                  {
  
                      SqlCommand maxMessId = new SqlCommand("SELECT Max(MessageID) FROM BlogMessages", conn);
                      lastMessageID = Convert.ToInt32(maxMessId.ExecuteScalar());
  
                      foreach (var uploadedFile in imageUpload.PostedFiles)
                      {
  
  
  
                          SqlCommand cmdInsertImage = new SqlCommand("INSERT INTO BlogImages(Image, MessageID) VALUES (@Image, @MessageID)", conn);
  
                          cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imageUpload.FileBytes;
                          cmdInsertImage.Parameters.AddWithValue("@MessageID", lastMessageID);
  
  
                          cmdInsertImage.ExecuteNonQuery();
  
                      }
                  }
  </code></pre>
  
  <p>I am thinking the issue could be with:</p>
  
  <pre><code>cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imageUpload.FileBytes; 
  </code></pre>
  
  <p>If that is getting the file bytes for only one image.. I am not sure how to get the filebytes for both files. The image column in my BlogImages table is of the type Image.</p>
  
  <p>Any suggestions are much appreciated!</p>