title: Image from SQL Server Table to asp.net image control
body: <pre><code>SqlCommand cmd = new SqlCommand("select top 1(CMTID),COMMENT,HEADING from FTAB ORDER BY CMTID DESC", conn);
  conn.Open();
  
  SqlDataReader sdr = cmd.ExecuteReader();
  
  if (sdr.Read() == true)
  {
      lblHead.Text = sdr["HEADING"].ToString();
      lblData.Text = sdr["COMMENT"].ToString();
  
  }
  
  conn.Close();
  </code></pre>
  
  <p>That is the code for normal data value retrieve from table now I want to get picture from SQL Server that is saved as <code>Binary</code> data that code mention in bottom. So I want to retrieve picture into an <code>Image</code> control of asp.net; please guide me.</p>
  
  <pre><code>if (FileUpload1.HasFile)
  {
          conn.Close();
  
          String SqlQery;
          SqlQery = "select max(CMTID) from FTAB";
  
          SqlCommand cmdid = new SqlCommand(SqlQery, conn);
          conn.Open();
  
          MaxID = (int)(cmdid.ExecuteScalar()) + 1;
  
          conn.Close();
  
          byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
          HttpPostedFile myimg = FileUpload1.PostedFile;
          myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
          SqlCommand cmd = new SqlCommand("insert into FTAB (CMTID, IMAGEDT, COMMENT, DATETM, HEADING) values (@imgid, @image, @comment, @datetm, @heading)", conn);
  
          SqlParameter imgid = new SqlParameter("@imgid",SqlDbType.Int);
          imgid.Value = MaxID;
          cmd.Parameters.Add(imgid);
  
          SqlParameter uploading = new SqlParameter("@image", SqlDbType.Image);
          uploading.Value = img;
          cmd.Parameters.Add(uploading);
  
          SqlParameter cmtt = new SqlParameter("@comment", SqlDbType.NVarChar);
          cmtt.Value = RadTextBox3.Text;
          cmd.Parameters.Add(cmtt);
  
          SqlParameter dttm = new SqlParameter("@datetm", SqlDbType.DateTime);
          dttm.Value = DateTime.Now;
          cmd.Parameters.Add(dttm);
  
          SqlParameter hhding = new SqlParameter("@heading", SqlDbType.NVarChar);
          hhding.Value = RadTextBox8.Text;
          cmd.Parameters.Add(hhding);
  
          conn.Open();
          cmd.ExecuteNonQuery();
          conn.Close();
  
          lblHead.Text = "Image uploaded";
      }
      else
      {
          lblHead.Text = "No file selected";
      }
  </code></pre>