private void textBoxCodigo_KeyUp(object sender, KeyEventArgs e)
 {
     using (SqlConnection sqlConn =
             new SqlConnection("Data Source=(localdb)\\Estoque;Initial Catalog=northwind;Integrated Security=True"))
     {
         using (SqlCommand sqlCommand = new SqlCommand())
         {
             sqlCommand.Parameters.AddWithValue("PId", textBoxCodigo.Text);
             sqlCommand.CommandText = "SELECT ProductID, ProductName," +
             "SupplierID, UnitPrice, UnitsInStock FROM Products WHERE ProductID= @PId";
             sqlCommand.Connection = sqlConn;
             sqlConn.Open();
             SqlDataReader dataReader;
             dataReader = sqlCommand.ExecuteReader(); //para caso que retorna registros
             if (dataReader.Read()) //encontrou registro
             {
                 prod = new Produto(Int32.Parse(dataReader["ProductID"].ToString()),
                 dataReader[1].ToString(),
                 Int32.Parse(dataReader[2].ToString()),
                 Decimal.Parse(dataReader[3].ToString()),
                 Double.Parse(textBoxQuantidade.Text));
                 //colocando os dados no formulario
                 textBoxDescricao.Text = prod.Nome;
             }
             else
             {
                 textBoxDescricao.Text = "";
                 prod = null;
             }
             sqlConn.Close();
         }
     }
 }
 private void buttonSalvar_Click(object sender, EventArgs e)
 {
     using (SqlConnection sqlConn =
             new SqlConnection("Data Source=(localdb)\\Estoque;Initial Catalog=northwind;Integrated Security=True"))
     {
         using (SqlCommand sqlCommand = new SqlCommand())
         {
             sqlCommand.Parameters.AddWithValue("PId", textCod.Text);
             sqlCommand.CommandText = "SELECT count(ProductID)" +
                 "FROM Products WHERE ProductID= @PId";
             sqlCommand.Connection = sqlConn;
             sqlConn.Open();
             SqlDataReader dataReader;
             dataReader = sqlCommand.ExecuteReader(); //para caso que retorna registros
             if (dataReader.Read()) //encontrou registro
             {
                 if(Int32.Parse(dataReader[0].ToString()) == 1)
                 {
                     sqlConn.Close();
                     p = new Produto(Int32.Parse(textCod.Text.ToString()),
                         textName.Text.ToString(),
                         Int32.Parse(textIdFor.Text.ToString()),
                         Decimal.Parse(textValorUni.Text.ToString()),
                         Double.Parse(textQuantidade.Text));
                     sqlCommand.CommandText = "UPDATE Products SET ProductName = '" + p.Nome +"'," +
                         "SupplierID = '" + p.IdFornecedor + "', UnitPrice = '" + p.PrecoUnitarioVenda + "'," +
                         "UnitsInStock = '" + p.Quantidade + "' where ProductID = '" + p.Id + "'";
                     sqlConn.Open();
                     if (sqlCommand.ExecuteNonQuery() > 0)
                     {
                         MessageBox.Show("Produto atualizado com sucesso!");
                     }
                     else
                     {
                         MessageBox.Show("Falha ao atualizar");
                     }
                     sqlConn.Close();
                 }
                 else
                 {
                     sqlConn.Close();
                     p = new Produto(Int32.Parse(textCod.Text.ToString()),
                         textName.Text.ToString(),
                         Int32.Parse(textIdFor.Text.ToString()),
                         Decimal.Parse(textValorUni.Text.ToString()),
                         Double.Parse(textQuantidade.Text));
                     sqlCommand.CommandText = "INSERT into Products (ProductName,SupplierID," +
                         "UnitPrice,UnitsInStock) values ('" + p.Nome+ "', '" + p.IdFornecedor +
                         "', '" + p.PrecoUnitarioVenda + "', '" + p.Quantidade +"')";
                     sqlConn.Open();
                     if (sqlCommand.ExecuteNonQuery() > 0)
                     {
                         MessageBox.Show("Produto adicionado com sucesso!");
                     }
                     else
                     {
                         MessageBox.Show("Produto nao adicionado");
                     }
                     sqlConn.Close();
                 }
             }
             else
             {
                 textCod.Text = "";
                 textName.Text = "";
                 textIdFor.Text = "";
                 textValorUni.Text = "";
                 textQuantidade.Text = "";
             }
             sqlConn.Close();
             bloquearTextBox();
         }
     }
 }