有时将项目加倍

时间:2018-02-04 20:08:55

标签: c#

我想添加一个代码,如果项目brcode已经存在,将添加按钮添加到Stock我编写了代码,但是第一次用户执行添加按钮时它工作并将其添加到股票但是当用户再次按下它添加到股票+添加具有相同值的新项目。它真的很讨厌我一整天都在努力寻找一种方法来自己做(我还在学习)。 我缺少什么

这是代码

private void btnadd_Click(object sender, EventArgs e)
    {

        int addedstock;
        string stock = "";
        cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code and Item_Stock = @Stock", con);
        cmd.Parameters.AddWithValue("@Code",codeadd.Text);
        cmd.Parameters.AddWithValue("@Stock", stockadd.Text);

        con.Open();        
        int result = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        if (result >= 1)
        {                


            DataGridViewRow row = new DataGridViewRow();
            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)
            {
                cmd = new SqlCommand("SELECT * FROM Products", con);
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    stock = Convert.ToString(reader.GetInt32(4));
                }
                con.Close();

                addedstock = Convert.ToInt32(stockadd.Text) + Convert.ToInt32(stock);




                cmd = new SqlCommand("UPDATE Products SET Item_Stock = @Stock WHERE Item_Code=@Code", con);
                cmd.Parameters.Add(new SqlParameter("@Stock", addedstock));
                cmd.Parameters.Add(new SqlParameter("@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 if (result != 1)
        {
            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 = Convert.ToString(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;
        }
        else
        {
            MessageBox.Show("Something went wrong", "Item Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

谢谢

1 个答案:

答案 0 :(得分:0)

您不仅要检查是否存在具有相同代码但具有相同库存的产品,只要数据库中的库存与您要添加查询的库存不同,将会失败:

cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code and Item_Stock = @Stock", con);

如果您添加了两个单元,然后尝试再添加一个(每个示例),该查询将返回0,您将添加一个重复的项目。

要更正这一点,只需从查询中删除库存数量:

cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code", con);

此外,我会在Item_Code上创建一个唯一索引,以避免重复,并且还允许您直接在一个查询中创建ON DUPLICATE KEY UPDATE来创建或更新产品。