我想做一个代码,如果brcode是相同的,那么将添加商品库存而不是添加新商品,但我总是最终添加库存并添加新商品。我只想要添加股票......
这是代码。
private void btnadd_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToString(row.Cells[3].Value) == codeadd.Text)
{
DialogResult dialogResult = MessageBox.Show("an Item with BrCode already exists. Do you with to add to the stock?", "Item Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
int addedstock = (Convert.ToInt32(row.Cells[4].Value) + Convert.ToInt32(stockadd.Text));
cmd = new SqlCommand("UPDATE Products SET Item_Stock = @Stock WHERE Item_Code=@Item_Code", con);
cmd.Parameters.Add(new SqlParameter("@Stock", addedstock));
cmd.Parameters.Add(new SqlParameter("@Item_Code", codeadd.Text));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
dt.Clear();
sda = new SqlDataAdapter("Select * From Products", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
else
{
cmd = new SqlCommand("INSERT INTO Products Values(@Item_Name, @Item_Price, @Item_Code, @Item_Stock, @Item_ExpDate)", con);
SqlParameter[] param = new SqlParameter[5];
param[0] = new SqlParameter("@Item_Name", SqlDbType.VarChar, 50);
param[0].Value = nameadd.Text;
param[1] = new SqlParameter("@Item_Price", SqlDbType.VarChar, 50);
param[1].Value = priceadd.Text;
param[2] = new SqlParameter("@Item_Code", SqlDbType.VarChar, 50);
param[2].Value = codeadd.Text;
param[3] = new SqlParameter("@Item_Stock", SqlDbType.Int);
param[3].Value = Convert.ToInt32(stockadd.Text);
param[4] = new SqlParameter("@Item_ExpDate", SqlDbType.DateTime);
param[4].Value = expdateadd.Text;
con.Open();
cmd.Parameters.AddRange(param);
cmd.ExecuteNonQuery();
con.Close();
dt.Clear();
sda = new SqlDataAdapter("Select * From Products", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
谢谢