title: c# updating datagridview using sqlcommand
body: <p>I've tried to update my datagridview. (edited)</p>
  
  <p>My code:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          con = new System.Data.SqlClient.SqlConnection();
          con.ConnectionString = "Data Source=tcp:SHEN-PC,49172\\SQLEXPRESS;Initial Catalog=LSEStock;Integrated Security=True";
          con.Open();
  
  
          if (dataGridView1.Rows.Count &gt; 0)
          {   
              int nRowIndex = dataGridView1.Rows.Count-2;
  
              if (dataGridView1.Rows[nRowIndex].Cells[1].Value != null)
              {
                  textBox2.Text = Convert.ToString(dataGridView1.Rows[nRowIndex].Cells[1].Value);
                  String updateData = "UPDATE CostPrice SET SupplierName = @SupplierName, CostPrice = @CostPrice WHERE PartsID = '" +textBox1.Text+"'";
                  SqlCommand update = new SqlCommand(updateData, con);
                  SqlDataAdapter adapter = new SqlDataAdapter(updateData, con);
  
                  update.Parameters.Add("@SupplierName", SqlDbType.NVarChar, 50, "SupplierName");
                  update.Parameters.Add("@CostPrice", SqlDbType.NVarChar, 50, "CostPrice");
                  adapter.UpdateCommand = update;
  
                  //update.ExecuteNonQuery();
  
                  if (update != null)
                  {
                      update.Dispose();
                      update = null;
                  }
  
              }
              else
              {
                  MessageBox.Show("NULL");
              }
          }
          con.Close();
      }
  </code></pre>
  
  <p>It says that ExecuteNonQuery is not initialized. What's wrong with my codes?</p>
  
  <p>I'm using SqlCommand to update, but what I see from the internet almost everyone is using SqlDataAdapter, what's the difference? Thanks in advance.</p>
  
  <p>If you have a better code, I would like to learn from that. Thanks!</p>