How to deduct product quantity from database after order is proceed

Jun 18 2021 12:13 PM

I have 2 asp.net WebForms, WebForm1 contains a button that redirects into WebForm2 which contains a contact form that needs to be filled to proceed an order. I have a drop down list in it that is connected to the database, and depending on which product a button on the WebForm1 is clicked, the current quantity is displayed from the specific product from the database. After the ordering, I need to decrease/deduct the product quantity from the database depending on how many products on the drop down list were selected.

How to decrease the product quantity after the order is proceed?

Here is the code that fills several TextBoxes, inserts currency and fills the drop down list "Quantity" depending on which product on the WebForm1 the button is clicked:

   protected void Page_Load(object sender, EventArgs e)
    {
        string productName = Request.QueryString["productname"];
        txt_product13.Text = productName;

        var dictionary = new Dictionary<string, object>
        {
    { "@ProductName", productName }
          };

        var parameters = new DynamicParameters(dictionary);
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        using (var connection = new SqlConnection(CS))
        {
            connection.Open();
            var sql = "SELECT * FROM ProductsDB WHERE ProductName = @ProductName";
            var product = connection.QuerySingle<Product>(sql, parameters);
            CultureInfo EuroCulture = new CultureInfo("fr-FR");
            txt_productprice.Text = product.Price.ToString("c", EuroCulture);

            for (int i = 1; i <= product.Quantity; i++)
            {
                dropdownlist1.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

        }
     
    }

For now I have this code for decreasing, can you tell where is my mistake and why does it not decrement the quantity from database?

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
string productName = Request.QueryString["productname"];
using (var connection = new SqlConnection(CS))
{
    connection.Open();
    var sql = "UPDATE ProductsDB SET Quantity = WHERE ProductName = @ProductName" + dropdownlist1.SelectedValue + "'";
    connection.Close();
}

Answers (14)