title: How to upload image to SQL Server using C#
body: <p>I originally had the data type for the image column in the database set to image, but I have since then changed it to VARBINARY(MAX).  However, when I attempt to run my code and upload an image to store in that column, every record is showing NULL.  I believe the database is fine and that the problem must be with how I am passing the image data into the SQL query.  So my question is this: What do I need to modify to get an image uploaded to the database in the correct format?  Image retrieval is planned to be implemented.</p>
  
  <p>Here is what the database looks like.  I included the column name, data type, and the results I get when selecting rows.  No errors were returned when I attempted to upload the image.  While the column DOES allow for null values, each of the records were attempts to upload an image.
  <img src="https://i.stack.imgur.com/a9F5w.jpg" alt="enter image description here"></p>
  
  <p>Below is the code for the "Add" button that handles communicating with the database.  I have made an attempt to cut out anything unrelated to this problem, such as closing the form.</p>
  
  <pre><code>private void addButton_Click(object sender, EventArgs e)
          {
              string strConnect = @"Server=mainserver\SQLEXPRESS; Database=Inventory; Integrated Security=SSPI;";
              SqlConnection con = new SqlConnection(strConnect);
              DatabaseUtility db = new DatabaseUtility();
              string sqlStart = @"INSERT INTO Vehicles(manufacturer, model, date_Acquired, vin, year, cost";
              string sqlEnd = @" VALUES('" + makeBox.Text + "', '" + modelBox.Text + "', '" + datePicker.Text + 
                                          "', '" + vinBox.Text + "', '" + yearBox.Text + "', '" + costBox.Text;
              string sql;
  
              if (vinBox.TextLength != 17)
              {
                  MessageBox.Show("The VIN entered is invalid", "Invalid VIN");
              }
              else if (String.IsNullOrWhiteSpace(makeBox.Text) || String.IsNullOrWhiteSpace(modelBox.Text) || 
                  String.IsNullOrWhiteSpace(yearBox.Text) || String.IsNullOrWhiteSpace(vinBox.Text) || 
                  String.IsNullOrWhiteSpace(costBox.Text))
              {
                  MessageBox.Show("Not all required fields are filled", "Missing Information");
              }
              else
              {
                  if (!String.IsNullOrWhiteSpace(askingBox.Text))
                  {
                      sqlStart += ", asking_Price";
                      sqlEnd += "', '" + askingBox.Text;
                  }
                  if (!String.IsNullOrWhiteSpace(categoryComboBox.Text))
                  {
                      sqlStart += ", category";
                      sqlEnd += "', '" + categoryComboBox.Text;
                  }
                  if (!String.IsNullOrWhiteSpace(additionalNotesBox.Text))
                  {
                      sqlStart += ", additional_Notes";
                      sqlEnd += "', '" + additionalNotesBox.Text;
                  }
                  if (!openFileDialog1.CheckFileExists)
                  {
                      sqlStart += @", image";
                      sqlEnd += "', '@image";
                  }
                  sqlStart += ")";
                  sqlEnd += "')";
                  sql = sqlStart + sqlEnd;
                  SqlCommand insertCommand = new SqlCommand(sql, con);
                  if (!openFileDialog1.CheckFileExists)
                  {
                      SqlParameter sqlParam = insertCommand.Parameters.AddWithValue("@image", (object)GetImage(openFileDialog1.FileName));
                      sqlParam.DbType = DbType.Binary;
                  }
                  try
                  {
                      MessageBox.Show(insertCommand.ToString(), "Invalid Input");
                      con.Open();
                      insertCommand.ExecuteNonQuery();
                      con.Close();
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("error in insertcommand" + ex, "Invalid Input");
                  }
                  finally
                  {
                      insertCommand.Connection.Close();
                  }
              }
          }
  </code></pre>