title: Image file from SQL data table to asp:Image control
body: <p>I have some problems with reading images form SQL and visualise in asp:Image control.
  The scenario is:</p>
  
  <ol>
  <li><p>Upload picture from local machine</p></li>
  <li><p>Website turns image file to bit array and save it</p></li>
  <li><p>Then read it from the table and parse it to specific asp:Image</p></li>
  </ol>
  
  <p>I have the following code:</p>
  
  <pre><code>&lt;asp:Content ID="wrapperContent" ContentPlaceHolderID="wrapper" Runat="Server"&gt;
  
      &lt;asp:Image ID="profileImage" runat="server" ImageUrl="&lt;%=img %&gt;" /&gt;&lt;br /&gt;
  
      &lt;asp:Button ID="ChangeImage" runat="server" Text="Change Photo" OnClick="ChangeImage_Click" /&gt;
  
      &lt;asp:FileUpload ID="FileUpload" runat="server" Visible="false" /&gt;
  
      &lt;asp:Button ID="UploadImage" runat="server" Text="Upload Photo" visible="false" OnClick="UploadImage_Click"/&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>And code behind:</p>
  
  <pre><code>public partial class Details : System.Web.UI.Page
  {
      public int id;    
      public Bitmap bitmap;
      public string imgUrl;
  
  
      protected void Page_Load(object sender, EventArgs e)
      {   
  
          if (Session["userName"] != null)
          {
              string sql = "select * from users where username='" + Session["userName"]+"'";
              SqlDataReader sdr = operateData.getRow(sql);
              sdr.Read();
              id = Int32.Parse(sdr["Id"].ToString());
  
              sql = "select * from profiles where userId='" + id+"'";  
              SqlDataReader sdrPr = operateData.getRow(sql);
              sdrPr.Read();
  
          SqlConnection con = operateData.createCon();
              con.Open();
              SqlDataAdapter sda = new SqlDataAdapter(sql, con);
              DataTable dt = new DataTable();
              sda.Fill(dt);
              DataRow row = dt.Rows[0];
              byte[] imgBytes = (byte[])row["img"];
              System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
  
              string filePath = Server.MapPath("temp") + "//" + "img" + DateTime.Now.Ticks.ToString() + ".png";
              FileStream fs = File.Create(filePath);
              fs.Write(imgBytes, 0, imgBytes.Length);
              fs.Flush();
              fs.Close();
  
              profileImage.ImageUrl = filePath;
  
          }
          else
          {
              Response.Redirect("Login.aspx");
          }       
      }
      protected void ChangeImage_Click(object sender, EventArgs e)
      {
          FileUpload.Visible = true;
          UploadImage.Visible = true;
      }
      protected void UploadImage_Click(object sender, EventArgs e)
      {
          if (FileUpload.HasFile)
          {
          string sql = "update profiles set img='" + img+"' where userId='" + id + "'";
              operateData.execSql(sql);            
          }
      }
  }
  </code></pre>
  
  <p>What do I miss..? how can I manage it?</p>