title: C# : how to upload image to a SQL Server database using varbinary(Max)
body: <p>I have been trying to upload images to my database, but I get this error: </p>
  
  <blockquote>
    <p>Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><code>con.Open();
  
  cmd = new SqlCommand("SELECT * FROM ImagePosts", con);
  ad = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  ad.Fill(ds);
  
  int id = ds.Tables[0].Rows.Count + 1;
  
  byte[] buffer = File.ReadAllBytes(file);
  
  SqlCommand cmd2 = new SqlCommand("INSERT INTO Images (Id, Title, Image) VALUES('" + id + "', '" + textBox1.Text + "', '" + "@image" + "')", con);
  
  var binary1 = cmd2.Parameters.Add("@image", SqlDbType.VarBinary, -1);
  binary1.Value = buffer;
  
  cmd2.ExecuteNonQuery();
  con.Close();
  this.Close();
  </code></pre>
  
  <p>Edit: my bad, I forgot to remove the parenthesis around @image.</p>