Question Decrement value from SQL database bug

Bojan123

New member
Joined
Jun 19, 2021
Messages
3
Programming Experience
Beginner
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, no matter what quantity is selected from the drop down list, it always decrements the quantity in the database by 1. Can anyone see my code and tell me where is my mistake? I tried many different approaches and methods but every time is the same, it only deducts the quantity by 1.

How to make deduction in the database by the same value that is selected from the drop down list?

Here is my code:

C#:
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))
        {
            int val = Convert.ToInt32(dropdownlist1.SelectedItem.Text);
            connection.Open();
            var sql = "UPDATE ProductsDB SET Quantity = Quantity - " + val + " WHERE ProductName = @ProductName";
            connection.Execute(sql, parameters);
            connection.Close();
        }

In case it matters, upper in the code I have the same approach for getting the currency, and getting the quantity from a specific product in a drop down list starting from 1 to its max quantity:

C#:
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()));
            }
 
Did you debug your code? If the value is being decremented by 1 then it must be that val is 1. You should be confirming that first. If that's the case then none of that code is really relevant and the issue is that the SelectedItem is not what you think it should be. If it's not the case then the SQL you think you're executing must not be what's actually being executed, unless something else is broken.

By the way, since when does a SqlConnection object have an Execute method?
 
Please post your questions in the most appropriate forum. This question has nothing to do with VS so it doesn't belong in the VS.NET General forum, which is for IDE questions. This is either a data access issue or a Web Forms issue. I'm guessing the latter and moving this thread there.
 
Did you debug your code? If the value is being decremented by 1 then it must be that val is 1. You should be confirming that first. If that's the case then none of that code is really relevant and the issue is that the SelectedItem is not what you think it should be. If it's not the case then the SQL you think you're executing must not be what's actually being executed, unless something else is broken.

By the way, since when does a SqlConnection object have an Execute method?
I just debbuged and the val is 1, you are right. Any ideas how to fix that?
Btw I was trying some other methods and I forgot to delete that :D
 
Make sure that your page load event handler is not accidentally overwriting the selected item when it's a post back.

If you are not use a post back, then how are you communicating between form 1 and form 2 so that form 2 picks up values selected from form 1?
 
The clearing method of the drop down list dropdownlist1.SelectedIndex = -1; upper in the code was the problem.
However, thank you guys.
 
Back
Top Bottom