title: Retrieving an image from SQL Server
body: <p>I wrote this code but faced an exception that said: <code>Parameter Not Valid.</code></p>
  
  <pre><code>    string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
  
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = connstr;
  
      conn.Open();
  
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;
  
      cmd.CommandText = "SELECT * FROM tbl";
  
      SqlDataAdapter da = new SqlDataAdapter();
      da.SelectCommand = cmd;
  
      DataTable dt = new DataTable();
      da.Fill(dt);
  
      byte[] barrImg = (byte[])dt.Rows[1]["image"];
  
      MemoryStream mstream = new MemoryStream();
  
      mstream.Write(barrImg, 0, barrImg.Length);
      mstream.Seek(0, SeekOrigin.Begin);
  
      img.Image = Image.FromStream(mstream);
      mstream.Close();
  </code></pre>
  
  <p>The exception is: </p>
  
  <blockquote>
    <p><code>Parameter is not valid.</code> </p>
  </blockquote>
  
  <p>in the <code>Image.FromStream</code> line of code.</p>
  
  <p>I checked the value that in datatable was assigned to the image field. It was <code>System.Byte[]</code>. I traced the code. Every thing seems to be correct. But it does not work.</p>
  
  <p>I searched around this problem. Another site preferred to set <code>mstream.Position = 0</code>. But that does not work.</p>
  
  <p>I stored my image by this code.If it maybe that I saved this wrong!</p>
  
  <pre><code>    string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
  
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = connstr;
  
      conn.Open();
  
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;
  
      string sql = "INSERT INTO tbl (name, family, image) VALUES ('name', 'family', '{0}')";
      sql = string.Format(sql, Image.FromFile("test.jpg"));
  
      cmd.CommandText = sql;
      cmd.ExecuteNonQuery();
      conn.Close();
  </code></pre>
  
  <p>new code to save the image:</p>
  
  <pre><code>public byte[] ReadFile(string sPath)
  {
      byte[] data = null;
  
  
      FileInfo fInfo = new FileInfo(sPath);
      long numBytes = fInfo.Length;
  
      FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
  
      BinaryReader br = new BinaryReader(fStream);
  
      data = br.ReadBytes((int)numBytes);
  
      return data;
  }
  </code></pre>
  
  <p>and :</p>
  
  <pre><code>private void cmdSave_Click(object sender, EventArgs e)
  {
      string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
  
        byte[] imageData = ReadFile("test.jpg");
  
        SqlConnection CN = new SqlConnection(connstr);
  
        string qry = "insert into tbl (name, family, image) VALUES ('name', 'family', '{0}')";
        qry = string.Format(qry, imageData);
  
        SqlCommand SqlCom = new SqlCommand(qry, CN);
  
        CN.Open();
        SqlCom.ExecuteNonQuery();
        CN.Close();
  }
  </code></pre>