title: Can I use varbinary type to store image in SQL Server database?
body: <p>I am trying to store an image in my SQL Server database, what datatype should I use?</p>
  
  <p>In the below <code>code aspx.cs</code>, I am trying to read all the bytes from the request inputstream and store it in the database, but the <code>byte[]</code> array is not updated properly in table. Am I missing something?</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
          Request.InputStream.Position = 0;
  
          byte[] Contents = new byte[Request.InputStream.Length];
  
          Request.InputStream.Read(Contents, 0, (int)Request.InputStream.Length);
  
          con.Open();
  
          try
          {
              string query = "update tblImageUpload set " + IMAGE_ID + " = @imageBytes where Image_ID='" + CID + "'";
  
              int i = 0;
  
              using (cmd = new SqlCommand(query, con))
              {
                  cmd.Parameters.Add("@imageBytes", SqlDbType.VarBinary, Contents.Length).Value = Contents;
  
                   i = cmd.ExecuteNonQuery();
              }
  
              Response.Write("Upload Query = " + query);
              Response.Write("Upload Code = " + i);
          } catch (Exception ex) {
              Response.Write("Upload Code=" + ex);
          }
  </code></pre>