title: Displaying multiple image retrieving from database
body: <p>My project allow admin to add medal for officer profile, currently I only able to insert the maximum of 5 medals. But my teacher ask me to let the admin insert as many medal as they want for the officer profile. I not sure how do I retrieve all the image that the admin inserted, I know how to insert image into database using varbinary. Do give me way for doing this. THANKS!</p>
  
  <p>Code Below is how I do for inserting at maximum of 5 medals:</p>
  
  <p>Code for uploading:</p>
  
  <pre><code>System.Drawing.Image uploaded = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
  
  System.Drawing.Image newImage = new Bitmap(1024, 768);
  using (Graphics g = Graphics.FromImage(newImage))
  {
      g.InterpolationMode = InterpolationMode.HighQualityBicubic;
      g.DrawImage(uploaded, 0, 0, 1024, 768);
  }
  
  byte[] results;
  using (MemoryStream ms = new MemoryStream())
  {
      ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c =&gt; c.FormatID == ImageFormat.Jpeg.Guid);
      EncoderParameters jpegParms = new EncoderParameters(1);
      jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
      newImage.Save(ms, codec, jpegParms);
      results = ms.ToArray();
  }
  
  string sqlImage = "Insert into OfficerMedal(policeid, image) values ('" + Session["policeid"] + "', @Data)";
  SqlCommand cmdImage = new SqlCommand(sqlImage);
  cmdImage.Parameters.AddWithValue("@Data", results);
  InsertUpdateData(cmdImage);
  </code></pre>
  
  <p>I retrieve image using aspx page</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          string strQuery = "select image from OfficerMedal where policeid='" + Session["policeid"] + "'";
          SqlCommand cmd = new SqlCommand(strQuery);
          DataTable dt = GetData(cmd);
          if (dt != null)
          {
              download(dt);
          }
      }
  
      private DataTable GetData(SqlCommand cmd)
      {
          DataTable dt = new DataTable();
          SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
          SqlDataAdapter sda = new SqlDataAdapter();
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          try
          {
              con.Open();
              sda.SelectCommand = cmd;
              sda.Fill(dt);
              return dt;
          }
          catch
          {
              return null;
          }
          finally
          {
              con.Close();
              sda.Dispose();
              con.Dispose();
          }
      }
  
      private void download(DataTable dt)
      {
          // check if you have any rows at all 
          // no rows -&gt; no data to convert!
          if (dt.Rows.Count &lt;= 0)
              return;
  
          // check if your row #0 even contains data -&gt; if not, you can't do anything!
          if (dt.Rows[0].IsNull("image"))
              return;
  
          Byte[] bytes = (Byte[])dt.Rows[0]["image"];
          Response.Buffer = true;
          Response.Charset = "";
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          Response.ContentType = "image/jpg";
          Response.BinaryWrite(bytes);
          Response.End();
      }
  </code></pre>
  
  <p>To add on for this current method I retrieving image 1 by 1 from database. Instead of retrieving all image that belong to the officer.</p>