title: How to insert uploaded image into varbinary(max) database column
body: <p>I want process form submit and save jpg image into varbinary sql column.
  I have code but it does not work properly, it is saving only empty bytes like <code>0x00...0000</code>. So no errors are raised and database row is inserted successfully, but varbinary column seems to me corrupted.</p>
  
  <p>The code is following</p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>public class FrontendModel
      {
          public HttpPostedFileBase Photo1 { get; set; }
      }
  
  public class SubmitModel
      {
          public byte[] ImageData { get; set; }
          public decimal ImageSizeB { get; set; }
  
          public SubmitModel
          (
              HttpPostedFileBase Photo
          )
          {
  
              this.ImageData = new byte[Photo.ContentLength];
              Photo.InputStream.Read(ImageData, 0, Convert.ToInt32(Photo.ContentLength));
  
          this.ImageSizeB = Photo.ContentLength;
      }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Index(FrontendModel m)
  {
  
      using (var db = new ABC.Models.ABCDBContext())
      {
  
          using (var scope = new TransactionScope())
          {
              if (m.Photo1 != null &amp;&amp; m.Photo1.ContentLength &gt; 0)
                  db.InsertSubmit(new SubmitModel(m.Photo1));
  
              scope.Complete();
          }
      }
  
      return View(new FrontendModel());
  }
  </code></pre>
  
  <p><strong>DB Insert Function</strong></p>
  
  <pre><code>public void InsertSubmit(SubmitModel m)
  {
      Database.ExecuteSqlCommand(
          "spInsertSubmit @p1",
          new SqlParameter("p1", m.ImageData),
      );
  }
  </code></pre>
  
  <p><strong>SQL DB Procedure</strong></p>
  
  <pre><code>CREATE PROCEDURE [dbo].[spInsertSubmit]
      @ImageData VARBINARY(max)
  AS
          INSERT INTO dbo.Images (Image)
          VALUES (@ImageData)
  </code></pre>
  
  <p>what am I doing wrong ? Thank you</p>
  
  <p>PS: </p>
  
  <p>I also tried something like this but it behave the same</p>
  
  <pre><code>using (var binaryReader = new BinaryReader(Photo.InputStream))
              {
                  this.ImageData = binaryReader.ReadBytes(Photo.ContentLength);
              }
  </code></pre>
  
  <p>then I tried</p>
  
  <pre><code>using (Stream inputStream = Photo.InputStream)
              {
                  MemoryStream memoryStream = inputStream as MemoryStream;
                  if (memoryStream == null)
                  {
                      memoryStream = new MemoryStream();
                      inputStream.CopyTo(memoryStream);
                  }
                  ImageData = memoryStream.ToArray();
              }
  </code></pre>
  
  <p>but error shows in calling DB function with error message, <code>Parameter is not valid</code></p>
  
  <p>i have the same problem as is mentioned here
  <a href="https://stackoverflow.com/questions/4127322/file-uploading-and-saving-to-database-incorrectly">File uploading and saving to database incorrectly</a></p>
  
  <p><a href="https://i.stack.imgur.com/xvZpr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvZpr.png" alt="enter image description here"></a></p>
  
  <p>I found that when i assign Input stream to memory stream, the memory stream is empty ?!</p>