title: Select multiple image files using fileupload control(ASP.net C#) and save it in database in one click.
body: <p>I am able to select multiple files using fileupload control but when I try to save it to a database it's giving me an "Object reference not set to an instance of an object" error. </p>
  
  <pre><code>if (FileUpload1.HasFiles)
  {
       foreach (HttpPostedFile uploaded in FileUpload1.PostedFiles)
       {
           bindata = new BinaryReader(uploaded.InputStream);
           ImageByteArray = bindata.ReadBytes(uploaded.ContentLength);
  
           // byte array is sent to a method
           dbmt.SaveImageToDB(ImageByteArray);
       }
  }
  </code></pre>
  
  <p>And the following is my code for the SaveImageToDB method</p>
  
  <pre><code>public void SaveImageToDB(byte[] ImageByteArray)
  {
      try
      {
          scon.Open();
          scm.Connection = scon;
  
          scm.CommandType = CommandType.StoredProcedure;
          scm.CommandText = "SaveProfileImage";
  
          SqlParameter paramImgArray = scm.Parameters.Add("@ImgBody", SqlDbType.Image,0);
          paramImgArray.Direction = ParameterDirection.Input;
          paramImgArray.Value = ImageByteArray;
  
          scm.ExecuteNonQuery();
      }
      catch( SqlException sqx )
      {
          throw sqx;
      }
  }
  </code></pre>