title: SQL Update with where clause with variables from VS2010 Winforms
body: <p>I am trying to do an update query from a winform with two variables without using a dataset.
  I assign both of my variable and then run the query but it keeps giving the error that zcomp is not a valid column name.  Which of course is true but I tell it which column before I say = zcomp.  Below is my code that is running the query.</p>
  
  <pre><code>Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
  Dim zcomp As Integer = gridRow.Cells(0).Value
  
  Dim con As New SqlConnection
  Dim cmd As New SqlCommand
  Try
    con.ConnectionString = "Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True"
    con.Open()
    cmd.Connection = con
    cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - zamnt WHERE [ComponentID] = zcomp"
    cmd.ExecuteNonQuery()
  Catch ex As Exception
    MessageBox.Show("Error while updating record on table..." &amp; ex.Message, "Update Records")
  Finally
    con.Close()
    gridRow.Cells(4).Value = "Yes"
  End Try
  </code></pre>
  
  <p>I have tried it several different ways.  It works just fine if I take out the zamnt and zcomp and put the actual number values that are in the variables.  Please help I've been searching all day for a way to use the variables with this update query.
  Thanks,
  Stacy</p>